Java BigDecimal movePointLeft()方法

Java BigDecimal movePointLeft()方法

java.math.BigDecimal.movePointLeft( int n ) 方法用于将当前BigDecimal的小数点向左移动n位。

  • 如果n为非负数,该调用只是将n加入到刻度中。
  • 如果n是负数,该调用等同于movePointRight(-n)。

该方法返回的BigDecimal值具有值(this × 10-n)和比例max(this.scale()+n, 0)。

语法

public BigDecimal movePointLeft(int n)

参数: 该方法需要一个整数类型的参数n,指的是小数点需要向左移动的位数。

返回值: 该方法返回相同的BigDecimal值,小数点向左移动了n位。

异常: 如果刻度溢出,该方法会抛出一个ArithmeticException。

示例

Input: value = 2300.9856, Leftshift = 3
Output: 2.3009856
**Explanation:**
while shifting the decimal point of 2300.9856 
by 3 places to the left, 2.3009856 is obtained
**Alternate way:** 2300.9856*10^(-3)=2.3009856

Input: value = 35001, Leftshift = 2
Output: 350.01

下面的程序说明了BigDecimal的movePointLeft()方法。

// Program to demonstrate movePointLeft() method of BigDecimal 
  
import java.math.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Create BigDecimal object
        BigDecimal bigdecimal = new BigDecimal("2300.9856");
  
        // Create a int i for decimal left move distance
        int i = 3;
  
        // Call movePointLeft() method on BigDecimal by shift i
        // Store the return value as BigDecimal
        BigDecimal changedvalue = bigdecimal.movePointLeft(i);
  
        String result = "After applying decimal move left by move Distance " 
        + i + " on " + bigdecimal + " New Value is " + changedvalue;
  
        // Print result
        System.out.println(result);
    }
}

输出。

After applying decimal move left by move Distance 3 on 2300.9856 New Value is 2.3009856

**参考资料: **https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#movePointLeft(int)

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程