Java-math-bigdecimal-movepointleft
提供:Dev Guides
Java.math.BigDecimal.movePointLeft()メソッド
説明
- java.math.BigDecimal.movePointLeft(int n)*は、小数点をn桁左に移動したものと同等のBigDecimalを返します。 nが負でない場合、呼び出しは単にnをスケールに追加します。 nが負の場合、呼び出しはmovePointRight(-n)と同等です。
この呼び出しによって返されるBigDecimalには値(this×10 ^ -n ^)とスケールmax(this.scale()+ n、0)があります。
宣言
以下は* java.math.BigDecimal.movePointLeft()*メソッドの宣言です。
public BigDecimal movePointLeft(int n)
パラメーター
*n* -小数点を左に移動する場所の数。
戻り値
このメソッドは、小数点をn桁左に移動したものと同等のBigDecimalを返します。
例外
*ArithmeticException* -スケールがオーバーフローした場合。
例
次の例は、math.BigDecimal.movePointLeft()メソッドの使用法を示しています。
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.movePointLeft(3);//3 points left
bg4 = bg2.movePointLeft(-2);//2 points right
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 0.12323
After moving the Decimal point 12323 is 1232300