For IntelliJ IDEA and
Android Studio


private void myMethod () {BigDecimal b =
String val;new BigDecimal(val)
Object value;new BigDecimal(value.toString())
BigDecimal.valueOf(unscaledVal)
- AI code suggestions by Codota
}
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(); }
@GwtIncompatible // DoubleMath.roundToBigInteger(double, RoundingMode) public void testRoundFractionalDoubleToBigInteger() { for (double d : FRACTIONAL_DOUBLE_CANDIDATES) { for (RoundingMode mode : ALL_SAFE_ROUNDING_MODES) { BigDecimal expected = new BigDecimal(d).setScale(0, mode); assertEquals(expected.toBigInteger(), DoubleMath.roundToBigInteger(d, mode)); } } }
public static BigDecimal parseHiveDecimal(byte[] bytes, int start, int length, DecimalType columnType) { BigDecimal parsed = new BigDecimal(new String(bytes, start, length, UTF_8)); if (parsed.scale() > columnType.getScale()) { // Hive rounds HALF_UP too parsed = parsed.setScale(columnType.getScale(), HALF_UP); } return rescale(parsed, columnType); } }
public static double round(double unrounded, int precision, int roundingMode) { BigDecimal bd = new BigDecimal(unrounded); BigDecimal rounded = bd.setScale(precision, roundingMode); return rounded.doubleValue(); }
@GwtIncompatible // DoubleMath.roundToInt(double, RoundingMode) public void testRoundExactIntegralDoubleToInt() { for (double d : INTEGRAL_DOUBLE_CANDIDATES) { BigDecimal expected = new BigDecimal(d).setScale(0, UNNECESSARY); boolean isInBounds = expected.compareTo(MAX_INT_AS_BIG_DECIMAL) <= 0 & expected.compareTo(MIN_INT_AS_BIG_DECIMAL) >= 0; try { assertEquals(expected.intValue(), DoubleMath.roundToInt(d, UNNECESSARY)); assertTrue(isInBounds); } catch (ArithmeticException e) { assertFalse(isInBounds); } } }
/** * @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)); }
new BigDecimal(value).setScale(places, RoundingMode.HALF_UP).doubleValue()
Double toBeTruncated = new Double("3.5789055"); Double truncatedDouble = BigDecimal.valueOf(toBeTruncated) .setScale(3, RoundingMode.HALF_UP) .doubleValue();
private static Slice internalDoubleToLongDecimal(double value, long precision, long scale) { if (Double.isInfinite(value) || Double.isNaN(value)) { throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast DOUBLE '%s' to DECIMAL(%s, %s)", value, precision, scale)); } try { // todo consider changing this implementation to more performant one which does not use intermediate String objects BigDecimal bigDecimal = BigDecimal.valueOf(value).setScale(intScale(scale), HALF_UP); Slice decimal = Decimals.encodeScaledValue(bigDecimal); if (overflows(decimal, intScale(precision))) { throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast DOUBLE '%s' to DECIMAL(%s, %s)", value, precision, scale)); } return decimal; } catch (ArithmeticException e) { throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast DOUBLE '%s' to DECIMAL(%s, %s)", value, precision, scale)); } }
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 }
@GwtIncompatible // DoubleMath.roundToLong(double, RoundingMode) public void testRoundExactIntegralDoubleToLong() { for (double d : INTEGRAL_DOUBLE_CANDIDATES) { // every mode except UNNECESSARY BigDecimal expected = new BigDecimal(d).setScale(0, UNNECESSARY); boolean isInBounds = expected.compareTo(MAX_LONG_AS_BIG_DECIMAL) <= 0 & expected.compareTo(MIN_LONG_AS_BIG_DECIMAL) >= 0; try { assertEquals(expected.longValue(), DoubleMath.roundToLong(d, UNNECESSARY)); assertTrue(isInBounds); } catch (ArithmeticException e) { assertFalse(isInBounds); } } }
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
BigDecimal bd = new BigDecimal(d).setScale(2, RoundingMode.HALF_EVEN); d = bd.doubleValue();
static double roundDigits(double x, int digits) { if(Double.isNaN(x)) return x; BigDecimal bd = new BigDecimal(x); bd = bd.setScale(digits, RoundingMode.HALF_EVEN); return bd.doubleValue(); } }
/** * Round a measure value by applying the scale defined on the metric. * Example: scale(0.1234) returns 0.12 if metric scale is 2 */ private static double scale(MetricDto metric, double value) { if (metric.getDecimalScale() == null) { return value; } BigDecimal bd = BigDecimal.valueOf(value); return bd.setScale(metric.getDecimalScale(), RoundingMode.HALF_UP).doubleValue(); }
@Test public void testMathExprBround() throws HiveException { double[] vArr = { 1.5, 2.5, -1.5, -2.5, 1.49, 1.51 }; for (double v : vArr) { double v1 = RoundUtils.bround(v, 0); double v2 = MathExpr.bround(v); Assert.assertEquals(v1, v2, 0.00001); double v3 = BigDecimal.valueOf(v).setScale(0, ROUND_HALF_EVEN).doubleValue(); Assert.assertEquals(v3, v2, 0.00001); } }
BigDecimal d = new BigDecimal("600.0").setScale(2, RoundingMode.HALF_UP).stripTrailingZeros(); System.out.println(d.toPlainString()); // Printed 600 for me
public static UserTrade adaptPoloniexUserTrade(PoloniexUserTrade userTrade, CurrencyPair currencyPair) { OrderType orderType = userTrade.getType().equalsIgnoreCase("buy") ? OrderType.BID : OrderType.ASK; BigDecimal amount = userTrade.getAmount(); BigDecimal price = userTrade.getRate(); Date date = PoloniexUtils.stringToDate(userTrade.getDate()); String tradeId = String.valueOf(userTrade.getTradeID()); String orderId = String.valueOf(userTrade.getOrderNumber()); // Poloniex returns fee as a multiplier, e.g. a 0.2% fee is 0.002 // fee currency/size depends on trade direction (buy/sell). It appears to be rounded down final BigDecimal feeAmount; final String feeCurrencyCode; if (orderType == OrderType.ASK) { feeAmount = amount.multiply(price).multiply(userTrade.getFee()).setScale(8, BigDecimal.ROUND_DOWN); feeCurrencyCode = currencyPair.counter.getCurrencyCode(); } else { feeAmount = amount.multiply(userTrade.getFee()).setScale(8, BigDecimal.ROUND_DOWN); feeCurrencyCode = currencyPair.base.getCurrencyCode(); } return new UserTrade(orderType, amount, currencyPair, price, date, tradeId, orderId, feeAmount, Currency.getInstance(feeCurrencyCode)); }