Java-math-bigdecimal-scalebypoweroften

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

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

説明

  • java.math.BigDecimal.scaleByPowerOfTen(int n)*は、数値が(this * 10 ^ n ^)に等しいBigDecimalを返します。 結果のスケールは(this.scale()-n)です。

宣言

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

public BigDecimal scaleByPowerOfTen(int n)

パラメーター

*n* -BigDecimalオブジェクトに10のべき乗を掛ける値。

戻り値

このメソッドは、値this * 10 ^ n ^のBigDecimalオブジェクトを返します。

例外

*ArithmeticException* -スケールが32ビット整数の範囲外になる場合。

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

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("235.000");
      bg2 = new BigDecimal("23500");

     //assign the result of method on bg1, bg2 to bg3, bg4
      bg3 = bg1.scaleByPowerOfTen(3);
      bg4 = bg2.scaleByPowerOfTen(-3);

      String str1 = bg1 + " raised to 10 power 3 is " +bg3;
      String str2 = bg2 + " raised to 10 power -3 is " +bg4;

     //print bg3, bg4 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

上記のプログラムをコンパイルして実行すると、次の結果が生成されます-

235.000 raised to 10 power 3 is 235000
23500 raised to 10 power -3 is 23.500