int scale()

优质
小牛编辑
122浏览
2023-12-01

描述 (Description)

java.math.BigDecimal.scale()返回此BigDecimal的比例。 如果为零或正数,则比例是小数点右侧的位数。

如果是负数,则将数字的未缩放值乘以10来表示比例的否定。 例如,比例为-3表示未缩放的值乘以1000。

声明 (Declaration)

以下是java.math.BigDecimal.scale()方法的声明。

public int scale()

参数 (Parameters)

NA

返回值 (Return Value)

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

异常 (Exception)

NA

例子 (Example)

以下示例显示了math.BigDecimal.scale()方法的用法。

package cn.xnip;
import java.math.*;
public class BigDecimalDemo {
   public static void main(String[] args) {
      // create 2 BigDecimal Objects
      BigDecimal bg1, bg2;
      bg1 = new BigDecimal("123.0");
      bg2 = new BigDecimal("-1.123");
      // create two int objects
      int i1,i2;
      // assign the result of scale on bg1, bg2 to i1,i2
      i1 = bg1.scale();
      i2 = bg2.scale();
      String str1 = "The scale of " + bg1 + " is " + i1;
      String str2 = "The scale of " + bg2 + " is " + i2;
      // print the values of i1,i2;
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

让我们编译并运行上面的程序,这将产生以下结果 -

The scale of 123.0 is 1
The scale of -1.123 is 3