BigDecimal.setScale
Code IndexAdd Codota to your IDE (free)

Best code snippets using java.math.BigDecimal.setScale(Showing top 20 results out of 3,249)

Refine search

  • BigDecimal.<init>
  • BigDecimal.doubleValue
  • PrintStream.println
  • BigDecimal.valueOf
  • BigDecimal.multiply
  • Common ways to obtain BigDecimal
private void myMethod () {
BigDecimal b =
  • String val;new BigDecimal(val)
  • Object value;new BigDecimal(value.toString())
  • BigDecimal.valueOf(unscaledVal)
  • AI code suggestions by Codota
}
origin: stackoverflow.com

 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();
}
origin: google/guava

@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));
  }
 }
}
origin: prestodb/presto

  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);
  }
}
origin: stackoverflow.com

 public static double round(double unrounded, int precision, int roundingMode)
{
  BigDecimal bd = new BigDecimal(unrounded);
  BigDecimal rounded = bd.setScale(precision, roundingMode);
  return rounded.doubleValue();
}
origin: google/guava

@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);
  }
 }
}
origin: prestodb/presto

/**
 * @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));
}

origin: stackoverflow.com

new BigDecimal(value).setScale(places, RoundingMode.HALF_UP).doubleValue()
origin: stackoverflow.com

 Double toBeTruncated = new Double("3.5789055");

Double truncatedDouble = BigDecimal.valueOf(toBeTruncated)
  .setScale(3, RoundingMode.HALF_UP)
  .doubleValue();
origin: prestodb/presto

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));
  }
}
origin: stackoverflow.com

 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
}
origin: google/guava

@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);
  }
 }
}
origin: stackoverflow.com

 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
origin: stackoverflow.com

 BigDecimal bd = new BigDecimal(d).setScale(2, RoundingMode.HALF_EVEN);
d = bd.doubleValue();
origin: h2oai/h2o-2

 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();
 }
}
origin: SonarSource/sonarqube

/**
 * 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();
}
origin: org.freemarker/freemarker

public Number multiply(Number first, Number second) {
  BigDecimal left = toBigDecimal(first);
  BigDecimal right = toBigDecimal(second);
  BigDecimal result = left.multiply(right);
  if (result.scale() > maxScale) {
    result = result.setScale(maxScale, roundingPolicy);
  }
  return result;
}
origin: apache/hive

@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);
 }
}
origin: stackoverflow.com

 BigDecimal d = new BigDecimal("600.0").setScale(2, RoundingMode.HALF_UP).stripTrailingZeros();
System.out.println(d.toPlainString()); // Printed 600 for me
origin: timmolter/XChange

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));
}
origin: apache/hbase

@Override
public BigDecimal multiply(BigDecimal bd1, BigDecimal bd2) {
 return (bd1 == null || bd2 == null) ? null : bd1.multiply(bd2)
   .setScale(2,RoundingMode.HALF_EVEN);
}
java.mathBigDecimalsetScale

Javadoc

Returns a new BigDecimal instance with the specified scale. If the new scale is greater than the old scale, then additional zeros are added to the unscaled value. If the new scale is smaller than the old scale, then trailing zeros are removed. If the trailing digits are not zeros then an ArithmeticException is thrown.

If no exception is thrown, then the following equation holds: x.setScale(s).compareTo(x) == 0.

Popular methods of BigDecimal

  • <init>
    Constructs a new BigDecimal instance from a string representation given as a character array. The re
  • valueOf
    Returns a new BigDecimal instance whose value is equal to unscaledVal * 10-scale ). The scale of the
  • compareTo
    Compares this BigDecimal with val. Returns one of the three values 1, 0, or -1. The method behaves a
  • doubleValue
    Returns this BigDecimal as a double value. If this is too big to be represented as an float, then Do
  • add
    Returns a new BigDecimal whose value is this + augend. The result is rounded according to the passed
  • divide
    Returns a new BigDecimal whose value is this / divisor. The scale of the result is the scale of this
  • multiply
    Returns a new BigDecimal whose value is this . The result is rounded according to the passed context
  • toString
    Returns a canonical string representation of this BigDecimal. If necessary, scientific notation is u
  • subtract
    Returns a new BigDecimal whose value is this - subtrahend. The result is rounded according to the pa
  • scale
    Returns the scale of this BigDecimal. The scale is the number of digits behind the decimal point. Th
  • longValue
    Returns this BigDecimal as an long value. Any fractional part is discarded. If the integral part of
  • intValue
    Returns this BigDecimal as an int value. Any fractional part is discarded. If the integral part of t
  • longValue,
  • intValue,
  • toBigInteger,
  • toPlainString,
  • equals,
  • unscaledValue,
  • negate,
  • floatValue,
  • hashCode

Popular classes and methods

  • getOriginalFilename (MultipartFile)
  • getSystemService (Context)
  • startActivity (Activity)
  • GridLayout (java.awt)
  • SocketException (java.net)
    This SocketException may be thrown during socket creation or setting options, and is the superclass
  • URL (java.net)
    Class URL represents a Uniform Resource Locator, a pointer to a "resource" on the World Wide Web.
  • Arrays (java.util)
    This class contains various methods for manipulating arrays (such as sorting and searching). This cl
  • BitSet (java.util)
    This implementation uses bit groups of size 32 to keep track of when bits are set to true or false.
  • Pattern (java.util.regex)
    Emulation of the Pattern class, uses RegExp as internal implementation.
  • JButton (javax.swing)

For IntelliJ IDEA and
Android Studio

  • Codota IntelliJ IDEA pluginCodota Android Studio pluginCode IndexSign in
  • EnterpriseFAQAboutContact Us
  • Terms of usePrivacy policyCodeboxFind Usages
Add Codota to your IDE (free)