BigDecimal byteValueExact() Method in Java Last Updated : 04 Dec, 2018 Comments Improve Suggest changes Like Article Like Report 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. Syntax: 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. Examples: Input : 127 Output : 127 Input : -67 Output : -67 Below programs will illustrate the use of byteValueExact() Function: Program 1: Java // Java program to demonstrate byteValueExact() method import java.io.*; import java.math.*; public class GFG { public static void main(String[] args) { // Creating a BigDecimal object BigDecimal b; // Creating a byte objects byte bt; b = new BigDecimal("47"); // Assigning the byte value of b to bt bt = b.byteValueExact(); // Displaying the byte value System.out.println("Exact byte value of " + b + " is " + bt); } } Output: Exact byte value of 47 is 47 Program 2: Java // Java program to demonstrate byteValueExact() method import java.io.*; import java.math.*; public class GFG { public static void main(String[] args) { // Creating a BigDecimal object BigDecimal b; b = new BigDecimal("-128.0564000"); System.out.println("BigDecimal value : " + b); long roundedValue = Math.round(b.doubleValue()); System.out.println("Rounded value : " + roundedValue); // Rounding is necessary as the fractional part is not zero // as well as exceeding the byte range of -128 to 127 b = new BigDecimal(roundedValue); System.out.println("Byte converted value : " + b.byteValueExact()); } } Output: BigDecimal value : -128.0564000 Rounded value : -128 Byte converted value : -128 Reference:https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#byteValueExact() Comment More infoAdvertise with us Next Article BigDecimal intvalueExact() Method in Java R RICHIK BHATTACHARJEE Follow Improve Article Tags : Misc Java Java-Functions java-math Java-BigDecimal Java-math-package +2 More Practice Tags : JavaMisc Similar Reads BigDecimal intvalueExact() Method in Java The java.math.BigDecimal.intValueExact() is an inbuilt function which converts this BigDecimal to an integer value as well as checks for the lost information. This function throws an Arithmetic Exception if there is any fractional part of this BigDecimal or if the result of the conversion is too big 3 min read BigDecimal intvalueExact() Method in Java The java.math.BigDecimal.intValueExact() is an inbuilt function which converts this BigDecimal to an integer value as well as checks for the lost information. This function throws an Arithmetic Exception if there is any fractional part of this BigDecimal or if the result of the conversion is too big 3 min read BigDecimal doubleValue() Method in Java The java.math.BigDecimal.doubleValue() is an in-built function which converts the BigDecimal object to a double. This function converts the BigDecimal to Double.NEGATIVE_INFINITY or Double.POSITIVE_INFINITY as appropriate or according to the passed object, if its magnitude is too big to be represent 2 min read BigDecimal doubleValue() Method in Java The java.math.BigDecimal.doubleValue() is an in-built function which converts the BigDecimal object to a double. This function converts the BigDecimal to Double.NEGATIVE_INFINITY or Double.POSITIVE_INFINITY as appropriate or according to the passed object, if its magnitude is too big to be represent 2 min read BigDecimal equals() Method in Java The java.math.BigDecimal.equals() method checks for equality of a BigDecimal value with the object passed. This method considers two BigDecimal objects equal if only if they are equal in value and scale. Syntax: public boolean equals(Object obj) Parameters: This function accepts an Object obj as a m 2 min read BigDecimal equals() Method in Java The java.math.BigDecimal.equals() method checks for equality of a BigDecimal value with the object passed. This method considers two BigDecimal objects equal if only if they are equal in value and scale. Syntax: public boolean equals(Object obj) Parameters: This function accepts an Object obj as a m 2 min read Like