Java.math.BigDecimal.longValueExact() 方法
描述
java.math.BigDecimal.longValueExact()将此 BigDecimal 转换为 long,检查丢失的信息。 如果此 BigDecimal 具有非零小数部分或超出长结果的可能范围,则引发 ArithmeticException。
声明
以下是 java.math.BigDecimal.longValueExact() 方法的声明。
public long longValueExact()
参数
NA
返回值
此方法返回 BigDecimal 对象的 long 值。
异常
ArithmeticException − 如果这有一个非零小数部分,或者不适合长。
示例
下面的例子展示了 math.BigDecimal.longValueExact() 方法的使用。
package com.tutorialspoint;
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 );
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
Exact Long value of -429 is -429 Exact Long value of 3.3E+10 is 33000000000

