
- Java.math 包附加內容
- Java.math - 列舉
- Java.math - 討論
Java.math.BigDecimal.shortValueExact() 方法
描述
java.math.BigDecimal.shortValueExact() 將此 BigDecimal 轉換為一個 short,並檢查丟失的資訊。如果此 BigDecimal 具有非零小數部分或超出 short 結果的可能範圍,則會丟擲 ArithmeticException。
宣告
以下是 java.math.BigDecimal.shortValueExact() 方法的宣告。
public short shortValueExact()
```java
引數
不適用
返回值
此方法返回 BigDecimal 物件的 short 值。
異常
ArithmeticException - 如果小數部分非零或不適合 short。
示例
package com.tutorialspoint; 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
讓我們編譯並執行以上程式,它將產生以下結果 -
列印頁面