常规中的大型默认比例 [英] default scale of bigdecimal in groovy

查看:162
本文介绍了常规中的大型默认比例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Groovy中BigDecimal的默认比例是多少?和舍入?



所以当试图做计算时:

  def x = 10.0 / 30.0 //0.3333333333 
def y = 20.0 / 30.0 //0.6666666667

在此基础上,我可以假设它使用比例尺10并四舍五入。
尽管找到官方文档有困难。

解决方案

您可以在官方文档中找到它:师运营商的案例



< blockquote>

5.5.1。除法运算符的情况



除法操作符/(和/ =除法和赋值)会产生
a双重结果,如果其中一个操作数为float或double,和
BigDecimal结果,否则(当两个操作数是
的整数类型short,char,byte,int,long,BigInteger或
BigDecimal的任何组合)。

使用divide()方法执行BigDecimal除法,如果
除法是精确的(即在相同精度和比例范围内产生可表示
的结果) ,或者使用一个
MathContext,其精度为两个操作数
precision 的最大值的精确度,加上10的额外精度和最大
10的比例和最大的操作数'scale

然后在 BigDecimalMath.java

  public Number divideImpl(Number left,Number right){
BigDecimal bigLeft = toBigDecimal(left);
BigDecimal bigRight = toBigDecimal(right);
尝试{
return bigLeft.divide(bigRight);
} catch(ArithmeticException e){
//设置DEFAULT精度,否则不终止
int precision = Math.max(bigLeft.precision(),bigRight.precision())+ DIVISION_EXTRA_PRECISION;
BigDecimal result = bigLeft.divide(bigRight,new MathContext(precision));
int scale = Math.max(Math.max(bigLeft.scale(),bigRight.scale()),DIVISION_MIN_SCALE);
if(result.scale()> scale)result = result.setScale(scale,BigDecimal.ROUND_HALF_UP);
返回结果;
}
}


What is the default scale of BigDecimal in groovy? And Rounding?

So when trying to do calculations:

def x = 10.0/30.0 //0.3333333333
def y = 20.0/30.0 //0.6666666667

Base on this, I can assume that it uses scale 10 and rounding half up. Having trouble finding an official documentation saying that though.

解决方案

You can find it in the official documentation: The case of the division operator

5.5.1. The case of the division operator

The division operators / (and /= for division and assignment) produce a double result if either operand is a float or double, and a BigDecimal result otherwise (when both operands are any combination of an integral type short, char, byte, int, long, BigInteger or BigDecimal).

BigDecimal division is performed with the divide() method if the division is exact (i.e. yielding a result that can be represented within the bounds of the same precision and scale), or using a MathContext with a precision of the maximum of the two operands' precision plus an extra precision of 10, and a scale of the maximum of 10 and the maximum of the operands' scale.

And check it in BigDecimalMath.java:

public Number divideImpl(Number left, Number right) {
    BigDecimal bigLeft = toBigDecimal(left);
    BigDecimal bigRight = toBigDecimal(right);
    try {
        return bigLeft.divide(bigRight);
    } catch (ArithmeticException e) {
        // set a DEFAULT precision if otherwise non-terminating
        int precision = Math.max(bigLeft.precision(), bigRight.precision()) + DIVISION_EXTRA_PRECISION;
        BigDecimal result = bigLeft.divide(bigRight, new MathContext(precision));
        int scale = Math.max(Math.max(bigLeft.scale(), bigRight.scale()), DIVISION_MIN_SCALE);
        if (result.scale() > scale) result = result.setScale(scale, BigDecimal.ROUND_HALF_UP);
        return result;
    }
}

这篇关于常规中的大型默认比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆