BigDecimal shortValueExact() Method in Java
The java.math.BigDecimal.shortValueExact() is an inbuilt method in java that 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.
Syntax:
public short shortValueExact()
Parameters: The method does not accepts any parameter.
Return value: This method returns the short value of the BigDecimal Object.
Below programs illustrates the above mentioned method:
Program 1:
// Program to demonstrate shortValueExact() method of BigDecimal import java.math.*; public class gfg { public static void main(String[] args) { BigDecimal b1 = new BigDecimal("457"); BigDecimal b2 = new BigDecimal("4785"); // Assigning the short value of BigDecimal objects b1 and b2 // to short s1, s2 respectively short s1 = b1.shortValueExact(); short s2 = b2.shortValueExact(); // Printing s1, s2 values System.out.println("Exact short value of " + b1 + " is " + s1); System.out.println("Exact short value of " + b2 + " is " + s2); } } |
Exact short value of 457 is 457 Exact short value of 4785 is 4785
Program 2:
// Program to demonstrate shortValueExact() method of BigDecimal import java.math.*; public class gfg { public static void main(String[] args) { BigDecimal b1 = new BigDecimal("127"); BigDecimal b2 = new BigDecimal("1455"); // assign the short value of BigDecimal objects b1 and b2 // to short s1, s2 respectively short s1 = b1.shortValueExact(); short s2 = b2.shortValueExact(); // print s1, s2 values System.out.println("Exact short value of " + b1 + " is " + s1); System.out.println("Exact short value of " + b2 + " is " + s2); } } |
Exact short value of 127 is 127 Exact short value of 1455 is 1455
Reference: https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#shortValueExact()
Recommended Posts:
- Java 8 | BigInteger shortValueExact() Method with Examples
- BigDecimal ulp() Method in Java
- BigDecimal max() Method in Java
- BigDecimal min() Method in Java
- BigDecimal abs() Method in Java
- BigDecimal plus() method in Java
- BigDecimal valueOf() Method in Java
- BigDecimal signum() Method in Java
- BigDecimal equals() Method in Java
- BigDecimal toBigInteger() Method in Java
- BigDecimal stripTrailingZeros() Method in Java
- BigDecimal toBigIntegerExact() Method in Java
- BigDecimal movePointLeft() Method in Java
- BigDecimal round() Method in Java
- BigDecimal longValueExact() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

