Java BigDecimal movePointRight()方法

Java BigDecimal movePointRight()方法

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

  • 如果n为非负数,该调用只是从刻度上减去n。
  • 如果n是负数,则该调用等同于movePointLeft(-n)。

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

语法

public BigDecimal movePointRight(int n)

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

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

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

例子

Input: value = 2300.9856, rightshift = 3
Output: 2300985.6
**Explanation:**
After shifting the decimal point of 2300.9856 by 3 places to right,
2300985.6 is obtained.
**Alternate way:** 2300.9856*10^(3)=2300985.6

Input: value = 35001, rightshift = 2
Output: 3500100

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

// Program to demonstrate movePointRight() 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 right move distance
        int i = 3;
  
        // Call movePointRight() method on BigDecimal by shift i
        BigDecimal changedvalue = bigdecimal.movePointRight(i);
  
        String result = "After applying decimal move right
        by move Distance " + i + " on " + bigdecimal + 
        " New Value is " + changedvalue;
  
        // Print result
        System.out.println(result);
    }
}

输出。

After applying decimal move right by move Distance 3 on 2300.9856 New Value is 2300985.6

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程