當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Java.math.BigDecimal.shortValueExact()用法及代碼示例


描述

這個java.math.BigDecimal.shortValueExact()將此 BigDecimal 轉換為 short,檢查丟失的信息。如果此 BigDecimal 具有非零小數部分或超出短結果的可能範圍,則拋出 ArithmeticException。

聲明

以下是聲明java.math.BigDecimal.shortValueExact()方法。

public short shortValueExact()

參數

NA

返回值

此方法返回 BigDecimal 對象的短值。

異常

ArithmeticException- 如果它有一個非零的小數部分,或者不適合短路。

示例

下麵的例子展示了 math.BigDecimal.shortValueExact() 方法的用法。

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

相關用法


注:本文由純淨天空篩選整理自 Java.math.BigDecimal.shortValueExact() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。