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.eveonline.api.repo.AccountBalanceTests.java

@Test
@Sql({ "/testGetAccountKey1000Balance.sql" })
public void testGetAccountKey1000Balance() {
    AccountBalance balance = repo.findByAccountKeyAndCorporationId(1000, 1l);
    BigDecimal expected = new BigDecimal(200000.35);
    expected = expected.setScale(2, BigDecimal.ROUND_HALF_UP);
    assertEquals("Incorrect balance for corp and account key", expected, balance.getBalance());
}

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

public BigDecimal getWorkHourForDay() {
    return getWorkHourForWeek().divide(BigDecimal.valueOf(getWorkDaysCount()), BigDecimal.ROUND_HALF_UP);
}

From source file:org.zapto.samhippiemiddlepoolchecker.Values.java

static public String valueToString(float value, int scale) {
    BigDecimal decimal = new BigDecimal(value);
    decimal = decimal.setScale(scale, BigDecimal.ROUND_HALF_UP);
    return decimal.toPlainString();
}

From source file:com.scf.core.EnvTest.java

public static double halfUp(double val, int scale) {
    if (scale < 0) {
        throw new IllegalArgumentException("The scale must be a positive integer or zero");
    }/*www .  ja  v  a2  s.c o m*/

    BigDecimal b = new BigDecimal(val);
    //?2??
    double f1 = b.setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    return f1;
}

From source file:net.vexelon.bgrates.Utils.java

public static String scaleNumber(BigDecimal number, int n) {
    return number.setScale(n, BigDecimal.ROUND_HALF_UP).toPlainString();
}

From source file:com.codecrate.shard.level.ConstantRateLevelCalculator.java

public int calculateValue(int level) {
    BigDecimal value = rate.multiply(new BigDecimal(level - 1));
    value = value.add(new BigDecimal(initialValue));
    value = value.setScale(0, BigDecimal.ROUND_HALF_UP);
    LOG.debug("Computed value for level " + level + " is: " + value);

    return value.intValue();
}

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

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

    vo.setId(po.getId());/* w ww .j  ava2 s  .co 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.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);
        }
    }

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

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

    return vo;
}

From source file:Main.java

/**
 * Adopted from http://jgnash.svn.sourceforge.net/viewvc/jgnash/jgnash2/trunk/src/jgnash/imports/qif/QifUtils.java
 *//*  w  w  w  .  ja v  a  2 s .c  o  m*/
public static long parseMoney(String money) {
    if (money != null) {
        BigDecimal bdMoney;
        money = money.trim(); // to be safe
        try {
            bdMoney = new BigDecimal(money);
            return moneyAsLong(bdMoney);
        } catch (NumberFormatException e) {
            /* there must be commas, etc in the number.  Need to look for them
             * and remove them first, and then try BigDecimal again.  If that
             * fails, then give up and use NumberFormat and scale it down
             * */
            String[] split = MONEY_PREFIX_PATTERN.split(money);
            if (split.length > 1) {
                StringBuilder buf = new StringBuilder();
                if (money.startsWith("-")) {
                    buf.append('-');
                }
                for (int i = 0; i < split.length - 1; i++) {
                    buf.append(split[i]);
                }
                buf.append('.');
                buf.append(split[split.length - 1]);
                try {
                    bdMoney = new BigDecimal(buf.toString());
                    return moneyAsLong(bdMoney);
                } catch (final NumberFormatException e2) {
                    Log.e("QifUtils", "Second parse attempt failed, falling back to rounding");
                }
            }
            NumberFormat formatter = NumberFormat.getNumberInstance();
            try {
                Number num = formatter.parse(money);
                BigDecimal bd = new BigDecimal(num.floatValue());
                if (bd.scale() > 6) {
                    bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
                }
                return moneyAsLong(bd);
            } catch (ParseException ignored) {
            }
            Log.e("QifUtils", "Could not parse money " + money);
        }
    }
    return 0;
}

From source file:ch.algotrader.util.RoundUtil.java

public static BigDecimal getBigDecimal(double value, int scale) {

    if (Double.isNaN(value) || Double.isInfinite(value)) {
        return null;
    } else {/*  w w w  .  ja v  a2s . com*/
        BigDecimal decimal = new BigDecimal(value);
        return decimal.setScale(scale, BigDecimal.ROUND_HALF_UP);
    }
}

From source file:Main.java

/**
 * Adopted from http://jgnash.svn.sourceforge.net/viewvc/jgnash/jgnash2/trunk/src/jgnash/imports/qif/QifUtils.java
 *//*from  w  w w. j a va 2 s  . c  o  m*/
public static long parseMoney(String money) {
    String sMoney = money;

    if (sMoney != null) {
        BigDecimal bdMoney;
        sMoney = sMoney.trim(); // to be safe
        try {
            bdMoney = new BigDecimal(sMoney);
            return moneyAsLong(bdMoney);
        } catch (NumberFormatException e) {
            /* there must be commas, etc in the number.  Need to look for them
             * and remove them first, and then try BigDecimal again.  If that
             * fails, then give up and use NumberFormat and scale it down
             * */
            String[] split = MONEY_PREFIX_PATTERN.split(sMoney);
            if (split.length > 2) {
                StringBuilder buf = new StringBuilder();
                if (sMoney.startsWith("-")) {
                    buf.append('-');
                }
                for (int i = 0; i < split.length - 1; i++) {
                    buf.append(split[i]);
                }
                buf.append('.');
                buf.append(split[split.length - 1]);
                try {
                    bdMoney = new BigDecimal(buf.toString());
                    return moneyAsLong(bdMoney);
                } catch (final NumberFormatException e2) {
                    Log.e("QifUtils", "Second parse attempt failed, falling back to rounding");
                }
            }
            NumberFormat formatter = NumberFormat.getNumberInstance();
            try {
                Number num = formatter.parse(sMoney);
                BigDecimal bd = new BigDecimal(num.floatValue());
                if (bd.scale() > 6) {
                    bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
                }
                return moneyAsLong(bd);
            } catch (ParseException ignored) {
            }
            Log.e("QifUtils", "Could not parse money " + sMoney);
        }
    }
    return 0;
}