Example usage for java.math BigDecimal ROUND_HALF_UP

List of usage examples for java.math BigDecimal ROUND_HALF_UP

Introduction

In this page you can find the example usage for java.math BigDecimal ROUND_HALF_UP.

Prototype

int ROUND_HALF_UP

To view the source code for java.math BigDecimal ROUND_HALF_UP.

Click Source Link

Document

Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up.

Usage

From source file:com.haulmont.timesheets.global.WorkTimeConfigBean.java

public BigDecimal getUserWorkHourForDay(User user) {
    return getUserWorkHourForWeek(user).divide(BigDecimal.valueOf(getWorkDaysCount()),
            BigDecimal.ROUND_HALF_UP);
}

From source file:com.konakart.util.TaxUtils.java

/**
 * Calculates the tax for one or more items
 * //  ww  w . jav  a2 s  .  c o m
 * @param taxRate
 *            tax rate as a percentage
 * @param cost
 *            cost of a single item
 * @param quantity
 *            Number of items
 * @param scale
 *            This is the scale used for the precision of the calculations. It is contained in
 *            the ADMIN_CURRENCY_DECIMAL_PLACES configuration variable.
 * @param rule
 *            The rule to be used which should be either TAX_PER_ITEM or TAX_ON_TOTAL.
 *            <ul>
 *            <li>
 *            TaxUtils.TAX_PER_ITEM : The tax is calculated for a single item, to the number of
 *            decimal places defined by scale. Then this value is multiplied by the quantity.
 *            <li>
 *            TaxUtils.TAX_ON_TOTAL : The tax is calculated for the total amount (single item
 *            cost x quantity).
 *            </ul>
 * @return Returns the tax amount
 * 
 */
public static BigDecimal getTaxAmount(BigDecimal taxRate, BigDecimal cost, int quantity, int scale, int rule) {
    if (taxRate == null || cost == null || quantity == 0) {
        return new BigDecimal(0);
    }

    BigDecimal lTaxRate = taxRate.divide(new BigDecimal(100));

    if (rule == TAX_PER_ITEM) {
        BigDecimal taxPerItem = cost.multiply(lTaxRate);
        taxPerItem = taxPerItem.setScale(scale, BigDecimal.ROUND_HALF_UP);
        BigDecimal totalTax = taxPerItem.multiply(new BigDecimal(quantity));
        totalTax = totalTax.setScale(scale, BigDecimal.ROUND_HALF_UP);
        return totalTax;
    } else if (rule == TAX_ON_TOTAL) {
        BigDecimal totalPrice = cost.multiply(new BigDecimal(quantity));
        BigDecimal totalTax = totalPrice.multiply(lTaxRate);
        totalTax = totalTax.setScale(scale, BigDecimal.ROUND_HALF_UP);
        return totalTax;
    }

    // Should never get this far
    return new BigDecimal(0);
}

From source file:mobile.vo.rns.RequireVO.java

public static RequireVO create(Require po) {
    ObjectMapper objectMapper = JackJsonUtil.getMapperInstance(false);
    RequireVO vo = new RequireVO();

    vo.setId(po.getId());/*from ww w.  j  a va 2 s  .  c  o  m*/
    vo.setOwnerUserId(po.getOwner().getId());
    vo.setOwnerUsername(po.getOwner().getUserNameOrEmail());
    vo.setOwnerAvatar_70(po.getOwner().getAvatar(70));

    Expert ownerExpert = po.getOwner().getExperts().iterator().next();
    vo.setOwnerJob(ownerExpert.job);
    vo.setOwnerCountry(ownerExpert.country);

    SkillTag industry = po.getIndustry();
    if (null != industry) {
        vo.setIndustryId(industry.getId());
        vo.setIndustryName(industry.getTagName());
    }

    vo.setTitle(po.getTitle());
    vo.setInfo(po.getInfo());

    if (null == po.getBudget()) {
        vo.setBudget("-1"); // -1 - ?
    } else {
        vo.setBudget(new BigDecimal(po.getBudget()).setScale(1, BigDecimal.ROUND_HALF_UP).toString());
    }

    vo.setCreateDate(new DateTime(po.getCreateDate()).toString("yyyy-MM-dd HH:mm:ss"));

    if (StringUtils.isNotBlank(po.getTags())) {
        try {
            @SuppressWarnings("unchecked")
            List<String> readValue = objectMapper.readValue(po.getTags(), List.class);
            vo.setTags(readValue);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    return vo;
}

From source file:org.bankinterface.util.Utils.java

/**
 * ??,??,,??.//w  w  w  .  j  av  a 2s .c o m
 * 
 * @param amount
 * @return
 */
public static String amountToFen(BigDecimal amount) {
    if (amount == null) {
        throw new IllegalArgumentException();
    }
    return amount.multiply(ONE_HUNDRED).setScale(0, BigDecimal.ROUND_HALF_UP).toPlainString();
}

From source file:Main.java

public static double getCpuUsage1() {
    try {//from w  ww  .  j ava  2s.c o  m
        RandomAccessFile reader = new RandomAccessFile("/proc/stat", "r");
        String load = reader.readLine();
        String[] toks = load.split(" ");

        double user1 = Double.parseDouble(toks[2]);
        double system1 = Double.parseDouble(toks[4]);
        double irq1 = Double.parseDouble(toks[7]);
        double idle1 = Double.parseDouble(toks[5]);
        try {
            Thread.sleep(360);
        } catch (Exception e) {
            e.printStackTrace();
        }
        reader.seek(0);
        load = reader.readLine();
        reader.close();
        toks = load.split(" ");
        double user2 = Double.parseDouble(toks[2]);
        double system2 = Double.parseDouble(toks[4]);
        double irq2 = Double.parseDouble(toks[7]);
        double idle2 = Double.parseDouble(toks[5]);

        double user_pass = user2 - user1;
        double system_pass = system2 - system1;
        double irq_pass = irq2 - irq1;
        double idle_pass = idle2 - idle1;
        double usage = (user_pass + system_pass + irq_pass) * 100.00
                / (user_pass + irq_pass + system_pass + idle_pass);
        BigDecimal b = new BigDecimal(usage);
        double res = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        return res;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e1) {
        e1.printStackTrace();
    }

    return 0;

}

From source file:com.sequenceiq.ambari.shell.flash.InstallProgress.java

@Override
public String getText() {
    StringBuilder sb = new StringBuilder();
    if (!done) {/* ww w  .j a v a2 s.  c om*/
        BigDecimal progress = client.getRequestProgress();
        if (progress != null) {
            BigDecimal decimal = progress.setScale(2, BigDecimal.ROUND_HALF_UP);
            int intValue = decimal.intValue();
            if (intValue != SUCCESS && intValue != FAILED) {
                sb.append("Installation: ").append(decimal).append("% ");
                int rounded = round(progress.setScale(0, BigDecimal.ROUND_UP).intValue() / 10);
                for (int i = 0; i < 10; i++) {
                    if (i < rounded) {
                        sb.append("=");
                    } else {
                        sb.append("-");
                    }
                }
            } else if (intValue == FAILED) {
                sb.append("Installation: FAILED");
                done = true;
            } else {
                sb.append("Installation: COMPLETE");
                done = true;
            }
        } else {
            sb.append("Installation: WAITING..");
        }
    } else {
        if (exit) {
            System.exit(0);
        }
    }
    return sb.toString();
}

From source file:mobile.vo.rns.ServiceDetailVO.java

public static ServiceDetailVO create(Service po) {
    ObjectMapper objectMapper = JackJsonUtil.getMapperInstance(false);
    ServiceDetailVO vo = new ServiceDetailVO();

    vo.setId(po.getId());//ww  w.  ja  v  a  2 s.  c o  m

    SkillTag industry = po.getIndustry();
    if (null != industry) {
        vo.setIndustryId(industry.getId());
        vo.setIndustryName(industry.getTagName());
    }

    vo.setTitle(po.getTitle());
    vo.setInfo(po.getInfo());

    if (null == po.getPrice()) {
        vo.setPrice("-1");// -1 - ?
    } else {
        vo.setPrice(new BigDecimal(po.getPrice()).setScale(1, BigDecimal.ROUND_HALF_UP).toString());
    }

    vo.setCreateDate(new DateTime(po.getCreateDate()).toString("yyyy-MM-dd HH:mm:ss"));

    vo.setCoverUrl(Assets.at(po.getCoverUrl()));

    if (StringUtils.isNotBlank(po.getTags())) {
        try {
            @SuppressWarnings("unchecked")
            List<String> readValue = objectMapper.readValue(po.getTags(), List.class);
            vo.setTags(readValue);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    Iterator<AttachOfService> iterator = po.getCaseAttachs().iterator();
    while (iterator.hasNext()) {
        AttachOfService attachOfService = iterator.next();
        HashMap<String, Object> pair = new HashMap<String, Object>();
        pair.put("attachId", attachOfService.id);
        pair.put("url", Assets.at(attachOfService.path));
        vo.getAttachs().add(pair);
    }

    Expert ownerExpert = po.getOwner().getExperts().iterator().next();
    vo.setOwner(User.create(ownerExpert));

    vo.setCommentInfo(CommentInfo.create(po));

    ServiceComment latestComment = ServiceComment.getLatestCommentByServiceId(po.getId());
    vo.setLatestComment(CommentItem.create(latestComment));

    return vo;
}

From source file:mobile.vo.rns.ServiceVO.java

public static ServiceVO create(Service po) {
    ObjectMapper objectMapper = JackJsonUtil.getMapperInstance(false);
    ServiceVO vo = new ServiceVO();

    vo.setId(po.getId());/*  w w w  .j av  a  2s. c om*/
    vo.setOwnerUserId(po.getOwner().getId());
    vo.setOwnerUsername(po.getOwner().getUserNameOrEmail());
    vo.setOwnerAvatar_70(po.getOwner().getAvatar(70));

    Expert ownerExpert = po.getOwner().getExperts().iterator().next();
    vo.setOwnerJob(ownerExpert.job);
    vo.setOwnerCountry(ownerExpert.country);

    SkillTag industry = po.getIndustry();
    if (null != industry) {
        vo.setIndustryId(industry.getId());
        vo.setIndustryName(industry.getTagName());
    }

    vo.setTitle(po.getTitle());
    vo.setInfo(po.getInfo());

    if (null == po.getPrice()) {
        vo.setPrice("-1");// -1 - ?
    } else {
        vo.setPrice(new BigDecimal(po.getPrice()).setScale(1, BigDecimal.ROUND_HALF_UP).toString());
    }

    vo.setCreateDate(new DateTime(po.getCreateDate()).toString("yyyy-MM-dd HH:mm:ss"));

    if (StringUtils.isNotBlank(po.getTags())) {
        try {
            @SuppressWarnings("unchecked")
            List<String> readValue = objectMapper.readValue(po.getTags(), List.class);
            vo.setTags(readValue);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    vo.setCoverUrl(Assets.at(po.getCoverUrl()));
    vo.setAverageScore(po.getAverageScore());
    vo.setCommentNum(po.getCommentNum());

    return vo;
}

From source file:com.salesmanager.core.module.impl.application.currencies.USDCurrencyModule.java

public String getMeasure(BigDecimal measure, String currencycode) throws Exception {

    NumberFormat nf = null;/*from w  ww .  j  av a 2 s.c  o  m*/

    nf = NumberFormat.getInstance(Locale.US);

    nf.setMaximumFractionDigits(1);
    nf.setMinimumFractionDigits(1);

    measure.setScale(1, BigDecimal.ROUND_HALF_UP);

    return nf.format(measure);

}

From source file:com.salesmanager.core.module.impl.application.currencies.EURCurrencyModule.java

public String getMeasure(BigDecimal measure, String currencycode) throws Exception {

    NumberFormat nf = null;/* www . ja v  a  2 s. c  o m*/

    nf = NumberFormat.getInstance(Locale.GERMAN);

    nf.setMaximumFractionDigits(1);
    nf.setMinimumFractionDigits(1);

    measure.setScale(1, BigDecimal.ROUND_HALF_UP);

    return nf.format(measure);
}