java.math.BigDecimal.setScale()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(8.5k)|赞(0)|评价(0)|浏览(327)

本文整理了Java中java.math.BigDecimal.setScale()方法的一些代码示例,展示了BigDecimal.setScale()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。BigDecimal.setScale()方法的具体详情如下:
包路径:java.math.BigDecimal
类名称:BigDecimal
方法名:setScale

BigDecimal.setScale介绍

[英]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.
[中]返回具有指定比例的新BigDecimal实例。如果新比例大于旧比例,则会向未缩放值添加额外的零。如果新比例小于旧比例,则删除尾随零。如果尾随数字不是零,则引发算术异常。
如果未引发异常,则以下等式成立:x.setScale(s)。比较(x)==0。

代码示例

代码示例来源: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));
}

相关文章