The java.math.BigDecimal.byteValueExact() is an in-built function which converts the BigDecimal to a byte and checks for lost information. Any BigDecimal value greater than 127 or less than -128, will generate an exception as it doesn’t fit in byte range.
public byte byteValueExact()
Parameters: The method does not accept any parameters.
Return Value:This method returns the byte value of the BigDecimal Object.
Exception: This function throws ArithmeticException in case if the BigDecimal has a nonzero fractional part, i.e., in case of a decimal values, or is out of the possible range for a byte result.
Input : 127 Output : 127 Input : -67 Output : -67
Below programs will illustrate the use of byteValueExact() Function:
Exact byte value of 47 is 47
BigDecimal value : -128.0564000 Rounded value : -128 Byte converted value : -128
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.