BigDecimal movePointLeft(int n)

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

描述 (Description)

java.math.BigDecimal.movePointLeft(int n)返回一个BigDecimal,它相当于一个小数点向左移动n位的BigDecimal。 如果n是非负数,则调用仅将n添加到比例。 如果n为负数,则该调用等效于movePointRight(-n)。

此调用返回的BigDecimal具有值(此×10 -n )和缩放最大值(this.scale()+ n,0)。

声明 (Declaration)

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

public BigDecimal movePointLeft(int n)

参数 (Parameters)

n - 向左移动小数点的位置数。

返回值 (Return Value)

此方法返回一个BigDecimal,它相当于一个小数点向左移动n位的BigDecimal。

异常 (Exception)

ArithmeticException - 如果scale溢出。

例子 (Example)

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

package cn.xnip;
import java.math.*;
public class BigDecimalDemo {
   public static void main(String[] args) {
      // create 4 BigDecimal objects
      BigDecimal bg1, bg2, bg3, bg4;
      bg1 = new BigDecimal("123.23");
      bg2 = new BigDecimal("12323");
      bg3 = bg1.movePointLeft(3); // 3 points left
      bg4 = bg2.movePointLeft(-2);// 2 points right
      String str1 = "After moving the Decimal point " + bg1 + " is " + bg3;
      String str2 = "After moving the Decimal point " + bg2 + " is " + bg4;
      // print bg3, bg4 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

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

After moving the Decimal point 123.23 is 0.12323
After moving the Decimal point 12323 is 1232300