%uA0java.math.BigDecimal.byteValueExact()%uA0转换将BigDecimal为一个字节,检查丢失的信息。如果此BigDecimal具有非零小数部分,或者超出的可能范围,字节的结果则抛出ArithmeticException。
声明
以下是java.math.BigDecimal.byteValueExact()方法的声明
public byte byteValueExact()
参数
-
NA
返回值
此方法返回的BigDecimal对象的字节值。
Exception
-
ArithmeticException%uA0- 如果BigDecimal对象的具有非零小数部分,或者不适合以字节形式。
例子
下面的示例演示math.BigDecimal.byteValueExact()方法的用法。
package com.yiibai import java.math.* public class BigDecimalDemo { public static void main(String[] args) { // create 2 BigDecimal objects BigDecimal bg1, bg2 // Create 2 byte objects byte i1, i2 // assign values to bg1 and bg2 bg1 = new BigDecimal("-128") bg2 = new BigDecimal("48") // any BigDecimal value greater than 127 or less than -128, // will generate exception as it doesn&apost fit in byte range // this method also generates exception for decimal values like 12.10 // assign the byte value of bg1 and bg2 to i1,i2 respectively i1 = bg1.byteValueExact() i2 = bg2.byteValueExact() String str1 = "Exact byte value of " + bg1 + " is " + i1 String str2 = "Exact byte value of " + bg2 + " is " + i2 // print i1,i2 values System.out.println( str1 ) System.out.println( str2 ) } }
让我们编译和运行上面的程序,这将产生以下结果:
Exact byte value of -128 is -128 Exact byte value of 48 is 48