Java.math.BigDecimal.shortValueExact() Method



The java.math.BigDecimal.shortValueExact() converts this BigDecimal to a short, checking for lost information. If this BigDecimal has a nonzero fractional part or is out of the possible range for a short result then an ArithmeticException is thrown.


Following is the declaration for java.math.BigDecimal.shortValueExact() method

public short shortValueExact()


  • NA

Return Value

This method returns the short value of the BigDecimal Object.


  • ArithmeticException - if this has a nonzero fractional part, or will not fit in a short


The following example shows the usage of math.BigDecimal.shortValueExact() method

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 );

Let us compile and run the above program, this will produce the following result:

Exact short value of 235 is 235
Exact short value of 4364 is 4364