Java Math BigDecimal.longValueExact() 方法


Java Math BigDecimal.longValueExact() 方法

package com.codingdict;



import java.math.*;



public class BigDecimalDemo {



   public static void main(String[] args) {



      // create 2 BigDecimal objects

      BigDecimal bg1, bg2;



      // create 2 long objecs

      long l1,l2;



      bg1 = new BigDecimal("-429");

      bg2 = new BigDecimal("3.3E+10");



      // assign the long value of bg1 and bg2 to l1,l2 respectively

      l1 = bg1.longValueExact();

      l2 = bg2.longValueExact();



      String str1 = "Exact Long value of " + bg1 + " is " + l1;

      String str2 = "Exact Long value of " + bg2 + " is " + l2;



      // print l1,l2 values

      System.out.println( str1 );

      System.out.println( str2 );

   }

}