Java-math-bigdecimal-movepointright

提供:Dev Guides
移動先:案内検索

Java.math.BigDecimal.movePointRight()メソッド

説明

  • java.math.BigDecimal.movePointRight(int n)*は、BigDecimalを返します。これは、小数点をn桁右に移動したものと同等です。 nが負でない場合、呼び出しは単にスケールからnを減算します。 nが負の場合、呼び出しはmovePointLeft(-n)と同等です。

この呼び出しによって返されるBigDecimalには値(this×10 ^ n ^)とスケールmax(this.scale()-n、0)があります。

宣言

以下は、* java.math.BigDecimal.movePointRight()*メソッドの宣言です。

public BigDecimal movePointRight(int n)

パラメーター

*n* -小数点を右に移動する場所の数。

戻り値

このメソッドは、小数点をn桁右に移動したものと同等のBigDecimalを返します。

例外

*ArithmeticException* -スケールがオーバーフローした場合。

次の例は、math.BigDecimal.movePointRight()メソッドの使用法を示しています。

package com.finddevguides;

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