Java中的BigDecimal scale()方法

这个 JAVA数学大十进制。比例尺 是java中的一个内置方法,返回 大十进制。

null
  • 对于 积极乐观的 数值时,刻度是小数点右边的位数。
  • 对于 消极的 值,则数字的未标度值乘以10,即标度的反幂。

语法:

public int scale()

参数: 此方法不接受任何参数。

返回值: 此方法返回此BigDecimal对象的比例。

以下程序说明了上述方法的工作原理:

项目1:

// Java program to demonstrate the
// scale() method
import java.math.*;
public class Gfg {
public static void main(String[] args)
{
BigDecimal b1 = new BigDecimal( "456.0" );
BigDecimal b2 = new BigDecimal( "-1.456" );
// Assign the result of scale on
// BigDecimal Objects b1, b2 to int objects i1, i2
int i1 = b1.scale();
int i2 = b2.scale();
// Print the values of i1, i2;
System.out.println( "The scale of " + b1 + " is " + i1);
System.out.println( "The scale of " + b2 + " is " + i2);
}
}


输出:

The scale of 456.0 is 1
The scale of -1.456 is 3

项目2:

// Java program to demonstrate the
// scale() method
import java.math.*;
public class Gfg {
public static void main(String[] args)
{
BigDecimal b1 = new BigDecimal( "745" );
BigDecimal b2 = new BigDecimal( "-174" );
// Assign the result of scale on
// BigDecimal Objects b1, b2 to int objects i1, i2
int i1 = b1.scale();
int i2 = b2.scale();
// Print the values of i1, i2;
System.out.println( "The scale of " + b1 + " is " + i1);
System.out.println( "The scale of " + b2 + " is " + i2);
}
}


输出:

The scale of 745 is 0
The scale of -174 is 0

参考: https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#scale()

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享