new BigDecimal(value).setScale(places, RoundingMode.HALF_UP).doubleValue()
public static double round(double value, int places) { if (places < 0) throw new IllegalArgumentException(); BigDecimal bd = new BigDecimal(value); bd = bd.setScale(places, RoundingMode.HALF_UP); return bd.doubleValue(); }
public static void main(String[] args) { String doubleVal = "1.745"; String doubleVal1 = "0.745"; BigDecimal bdTest = new BigDecimal( doubleVal); BigDecimal bdTest1 = new BigDecimal( doubleVal1 ); bdTest = bdTest.setScale(2, BigDecimal.ROUND_HALF_UP); bdTest1 = bdTest1.setScale(2, BigDecimal.ROUND_HALF_UP); System.out.println("bdTest:"+bdTest); //1.75 System.out.println("bdTest1:"+bdTest1);//0.75, no problem }
double r = 5.1234; System.out.println(r); // r is 5.1234 int decimalPlaces = 2; BigDecimal bd = new BigDecimal(r); // setScale is immutable bd = bd.setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP); r = bd.doubleValue(); System.out.println(r); // r is 5.12
Map<Long, BigDecimal> idPriceMap = new HashMap<>(); for (H2User u : userService.list(ew)) { System.out.println(u.getName() + "," + u.getAge() + "," + u.getVersion()); idPriceMap.put(u.getTestId(), u.getPrice()); if (u.getVersion() != null && u.getVersion() == 99) { System.out.println("============after update"); ew = new QueryWrapper<>(); ew.ge("age", AgeEnum.TWO.getValue()); for (H2User u : userService.list(ew)) { System.out.println(u.getName() + "," + u.getAge() + "," + u.getVersion()); if (u.getTestId().equals(id99)) { Assertions.assertEquals(100, u.getVersion().intValue(), "optLocker should update version+=1"); for (H2User u : userService.list(new QueryWrapper<>())) { System.out.println(u.getName() + "," + u.getAge() + "," + u.getVersion()); Assertions.assertEquals(u.getPrice().setScale(2, RoundingMode.HALF_UP).intValue(), BigDecimal.ZERO.setScale(2, RoundingMode.HALF_UP).intValue(), "all records should be updated");
public static long parseMicroCost(String costString) { BigDecimal costBigDecimal = new BigDecimal(costString); if (costBigDecimal.scale() > 11) { throw new IllegalArgumentException("The costString (" + costString + ") has a scale (" + costBigDecimal.scale() + ") higher than 10."); } costBigDecimal = costBigDecimal.setScale(11); return costBigDecimal.multiply(MICROS_PER_ONE_AS_BIG_DECIMAL).longValueExact(); }
Double toBeTruncated = new Double("3.5789055"); Double truncatedDouble = BigDecimal.valueOf(toBeTruncated) .setScale(3, RoundingMode.HALF_UP) .doubleValue();
/** * Using BigDecimal so we can throw if we are overflowing the Long.Max. * * @param first - First Num. * @param second - Second Num. * @return Returns a double */ private static double multiply(double first, double second) { BigDecimal firstVal = new BigDecimal(first); BigDecimal secondVal = new BigDecimal(second); return firstVal.multiply(secondVal) .setScale(PRECISION, RoundingMode.HALF_UP).doubleValue(); }
/** * @since 2.7.3 */ public static BigDecimal toBigDecimal(long seconds, int nanoseconds) { if (nanoseconds == 0L) { // 14-Mar-2015, tatu: Let's retain one zero to avoid interpretation // as integral number if (seconds == 0L) { // except for "0.0" where it can not be done without scientific notation return BigDecimal.ZERO.setScale(1); } return BigDecimal.valueOf(seconds).setScale(9); } return new BigDecimal(toDecimal(seconds, nanoseconds)); }
@Override public HardMediumSoftBigDecimalScore multiply(double multiplicand) { // Intentionally not taken "new BigDecimal(multiplicand, MathContext.UNLIMITED)" // because together with the floor rounding it gives unwanted behaviour BigDecimal multiplicandBigDecimal = BigDecimal.valueOf(multiplicand); // The (unspecified) scale/precision of the multiplicand should have no impact on the returned scale/precision return new HardMediumSoftBigDecimalScore( (int) Math.floor(initScore * multiplicand), hardScore.multiply(multiplicandBigDecimal).setScale(hardScore.scale(), RoundingMode.FLOOR), mediumScore.multiply(multiplicandBigDecimal).setScale(mediumScore.scale(), RoundingMode.FLOOR), softScore.multiply(multiplicandBigDecimal).setScale(softScore.scale(), RoundingMode.FLOOR)); }
BigDecimal a = new BigDecimal("123.13698"); BigDecimal roundOff = a.setScale(2, BigDecimal.ROUND_HALF_EVEN); System.out.println(roundOff);
static Double round(Double d, int precise) { BigDecimal bigDecimal = new BigDecimal(d); System.out.println("Before round: " + bigDecimal.toPlainString()); bigDecimal = bigDecimal.setScale(15, RoundingMode.HALF_UP); System.out.println("Hack round: " + bigDecimal.toPlainString()); bigDecimal = bigDecimal.setScale(precise, RoundingMode.HALF_UP); System.out.println("After round: " + bigDecimal.toPlainString()); return bigDecimal.doubleValue(); }
public static double round(double unrounded, int precision, int roundingMode) { BigDecimal bd = new BigDecimal(unrounded); BigDecimal rounded = bd.setScale(precision, roundingMode); return rounded.doubleValue(); }
public static BigDecimal round(float d, int decimalPlace) { BigDecimal bd = new BigDecimal(Float.toString(d)); bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP); return bd; }
@Override public List<MutablePair<String, String>> getIntervals(String lowerBound, String upperBound, int numPartitions, TypeInfo typeInfo) { List<MutablePair<String, String>> intervals = new ArrayList<>(); DecimalTypeInfo decimalTypeInfo = (DecimalTypeInfo)typeInfo; int scale = decimalTypeInfo.getScale(); BigDecimal decimalLower = new BigDecimal(lowerBound); BigDecimal decimalUpper = new BigDecimal(upperBound); BigDecimal decimalInterval = (decimalUpper.subtract(decimalLower)).divide(new BigDecimal(numPartitions), MathContext.DECIMAL64); BigDecimal splitDecimalLower, splitDecimalUpper; for (int i=0;i<numPartitions;i++) { splitDecimalLower = decimalLower.add(decimalInterval.multiply(new BigDecimal(i))).setScale(scale, RoundingMode.HALF_EVEN); splitDecimalUpper = decimalLower.add(decimalInterval.multiply(new BigDecimal(i+1))).setScale(scale, RoundingMode.HALF_EVEN); if (splitDecimalLower.compareTo(splitDecimalUpper) < 0) { intervals.add(new MutablePair<String, String>(splitDecimalLower.toPlainString(), splitDecimalUpper.toPlainString())); } } return intervals; } }
private static double scale(double value, int decimalScale) { BigDecimal bd = BigDecimal.valueOf(value); return bd.setScale(decimalScale, RoundingMode.HALF_UP).doubleValue(); } }
@Override public HardSoftBigDecimalScore multiply(double multiplicand) { // Intentionally not taken "new BigDecimal(multiplicand, MathContext.UNLIMITED)" // because together with the floor rounding it gives unwanted behaviour BigDecimal multiplicandBigDecimal = BigDecimal.valueOf(multiplicand); // The (unspecified) scale/precision of the multiplicand should have no impact on the returned scale/precision return new HardSoftBigDecimalScore( (int) Math.floor(initScore * multiplicand), hardScore.multiply(multiplicandBigDecimal).setScale(hardScore.scale(), RoundingMode.FLOOR), softScore.multiply(multiplicandBigDecimal).setScale(softScore.scale(), RoundingMode.FLOOR)); }