📜  Java中的 BigDecimal movePointRight() 方法

📅  最后修改于: 2022-05-13 01:54:42.248000             🧑  作者: Mango

Java中的 BigDecimal movePointRight() 方法

先决条件: BigDecimal 基础

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

  • 如果 n 为非负数,则调用仅从标度中减去 n。
  • 如果 n 为负数,则调用等效于 movePointLeft(-n)。

此方法返回的 BigDecimal 具有值 (this × 10n) 和 scale 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: Java/math/BigDecimal.html#movePointRight(int)