Java BigDecimal scale()方法
java.math.BigDecimal.scale() 是java中的一个内置方法,用于返回该BigDecimal的比例。
- 对于零或正值,比例是小数点右边的数字。
- 对于负值,该数字的未标度值将乘以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()