Tabnine Logo
BigDecimal.setScale
Code IndexAdd Tabnine to your IDE (free)

How to use
setScale
method
in
java.math.BigDecimal

Best Java code snippets using java.math.BigDecimal.setScale (Showing top 20 results out of 8,955)

Refine searchRefine arrow

  • BigDecimal.<init>
  • BigDecimal.doubleValue
  • PrintStream.println
  • BigDecimal.valueOf
  • BigDecimal.multiply
origin: stackoverflow.com

new BigDecimal(value).setScale(places, RoundingMode.HALF_UP).doubleValue()
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: apache/hbase

@Override
public BigDecimal multiply(BigDecimal bd1, BigDecimal bd2) {
 return (bd1 == null || bd2 == null) ? null : bd1.multiply(bd2)
   .setScale(2,RoundingMode.HALF_EVEN);
}
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: 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 d = BigDecimal.valueOf(1548.5649);
BigDecimal result = d.subtract(d.setScale(0, RoundingMode.FLOOR)).movePointRight(d.scale());      
System.out.println(result);
origin: baomidou/mybatis-plus

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");
origin: kiegroup/optaplanner

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

 Double toBeTruncated = new Double("3.5789055");

Double truncatedDouble = BigDecimal.valueOf(toBeTruncated)
  .setScale(3, RoundingMode.HALF_UP)
  .doubleValue();
origin: org.apache.hadoop/hadoop-common

/**
 * 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();
}
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: kiegroup/optaplanner

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

public static long bround(long input, int scale) {
 return BigDecimal.valueOf(input).setScale(scale, RoundingMode.HALF_EVEN).longValue();
}
origin: stackoverflow.com

 BigDecimal a = new BigDecimal("123.13698");
BigDecimal roundOff = a.setScale(2, BigDecimal.ROUND_HALF_EVEN);
System.out.println(roundOff);
origin: stackoverflow.com

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

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

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

 private static double scale(double value, int decimalScale) {
  BigDecimal bd = BigDecimal.valueOf(value);
  return bd.setScale(decimalScale, RoundingMode.HALF_UP).doubleValue();
 }
}
origin: kiegroup/optaplanner

@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));
}
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>
    Translates a character array representation of a BigDecimal into a BigDecimal, accepting the same se
  • valueOf
  • compareTo
    Compares this BigDecimal with the specified BigDecimal. Two BigDecimal objects that are equal in val
  • doubleValue
    Converts this BigDecimal to a double. This conversion is similar to thenarrowing primitive conversio
  • divide
  • add
  • multiply
  • toString
    Returns the string representation of this BigDecimal, using scientific notation if an exponent is ne
  • subtract
    Returns a BigDecimal whose value is (this - subtrahend), with rounding according to the context sett
  • intValue
    Converts this BigDecimal to an int. This conversion is analogous to thenarrowing primitive conversio
  • longValue
    Converts this BigDecimal to a long. This conversion is analogous to thenarrowing primitive conversio
  • toPlainString
    Returns a string representation of this BigDecimalwithout an exponent field. For values with a posit
  • longValue,
  • toPlainString,
  • scale,
  • toBigInteger,
  • equals,
  • floatValue,
  • negate,
  • unscaledValue,
  • abs

Popular in Java

  • Reading from database using SQL prepared statement
  • findViewById (Activity)
  • addToBackStack (FragmentTransaction)
  • scheduleAtFixedRate (ScheduledExecutorService)
  • Container (java.awt)
    A generic Abstract Window Toolkit(AWT) container object is a component that can contain other AWT co
  • Rectangle (java.awt)
    A Rectangle specifies an area in a coordinate space that is enclosed by the Rectangle object's top-
  • File (java.io)
    An "abstract" representation of a file system entity identified by a pathname. The pathname may be a
  • FileInputStream (java.io)
    An input stream that reads bytes from a file. File file = ...finally if (in != null) in.clos
  • PrintWriter (java.io)
    Wraps either an existing OutputStream or an existing Writerand provides convenience methods for prin
  • TimeUnit (java.util.concurrent)
    A TimeUnit represents time durations at a given unit of granularity and provides utility methods to
  • Best plugins for Eclipse
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now