Java.math.BigDecimal.movePointRight() 方法



說明

java.math.BigDecimal.movePointRight(int n) 返回一個 BigDecimal,它等效於將小數點向右移動 n 位的 BigDecimal。如果 n 為非負,則該方法僅僅將 n 從刻度中減去。如果 n 為負,則該方法等效於 movePointLeft(-n)。

此方法返回的 BigDecimal 的值為 (this × 10n),刻度為 max(this.scale()-n, 0)。

宣告

以下是 java.math.BigDecimal.movePointRight() 方法的宣告。

public BigDecimal movePointRight(int n)

引數

n − 將小數點向右移動的位數。

返回值

此方法返回一個 BigDecimal,它等效於將小數點向右移動 n 位的 BigDecimal。

異常

ArithmeticException − 如果刻度溢位。

示例

以下示例演示了 math.BigDecimal.movePointRight() 方法的使用。

package com.tutorialspoint;

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.movePointRight(3); // 3 places right
      bg4 = bg2.movePointRight(-2);// 2 places left

      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 123230
After moving the Decimal point 12323 is 123.23
java_math_bigdecimal.htm
廣告