Java-math-bigdecimal-tobigintegerexact

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

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

説明

  • java.math.BigDecimal.toBigIntegerExact()*は、このBigDecimalをBigIntegerに変換し、失われた情報をチェックします。 このBigDecimalにゼロ以外の小数部がある場合、例外がスローされます。

宣言

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

public BigInteger toBigIntegerExact()

パラメーター

NA

戻り値

このメソッドは、BigIntegerに変換されたBigDecimalオブジェクトの値を返します。

例外

*ArithmeticException* -これにゼロ以外の小数部がある場合。

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

package com.finddevguides;

import java.math.*;

public class BigDecimalDemo {

   public static void main(String[] args) {

     //create a BigDecimal object
      BigDecimal bg1;

     //create a BigInteger object
      BigInteger i1;

      bg1 = new BigDecimal("2426");

     //assign the BigIntegerExact value of bg1 to i1
      i1 = bg1.toBigIntegerExact();

      String str = "BigInteger value of " + bg1 + " is " + i1;

     //print i1 value
      System.out.println( str );
   }
}

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

BigInteger value of 2426 is 2426