java.math.BigDecimal.shortValueExact() 此BigDecimal 转换为short,检查丢失的信息。如果此BigDecimal具有非零小数部分,或者short的结果超出的可能范围,那么则抛出ArithmeticException。
声明
以下是java.math.BigDecimal.shortValueExact()方法的声明
public short shortValueExact()
参数
-
NA
返回值
此方法返回BigDecimal对象的short值。
异常
-
ArithmeticException - 如果这具有非零小数部分,或者将不适合使用short
例子
下面的例子显示math.BigDecimal.shortValueExact()方法的用法
package com.yiibai; import java.math.*; public class BigDecimalDemo { public static void main(String[] args) { // create 2 BigDecimal objects BigDecimal bg1, bg2; // create 2 short objects short s1, s2; bg1 = new BigDecimal("235"); bg2 = new BigDecimal("4364"); // assign the short value of bg1 and bg2 to s1,s2 respectively s1 = bg1.shortValueExact(); s2 = bg2.shortValueExact(); String str1 = "Exact short value of " + bg1 + " is " + s1; String str2 = "Exact short value of " + bg2 + " is " + s2; // print s1,s2 values System.out.println( str1 ); System.out.println( str2 ); } }
让我们编译和运行上面的程序,这将产生以下结果:
Exact short value of 235 is 235 Exact short value of 4364 is 4364