With this example, working of byteValueExact() method in getting exact byte value from a bigdecimal object is demonstrated.
Java BigDecimal byteValueExact example
With this example, working of byteValueExact() method in getting exact byte value from a
bigdecimal
object is demonstrated. This method transfigures(changes) a bigdecimal
object value into a byte data type value.
Method throws Arithmetic Exception in following two cases.
Firstly when the fractional part value of bigdecimal object is nonzero. Secondly if any how the value does not fit in byte, may be due to over flowing case in which the number surpasses (exceeds) the defined byte range ( -128 to 127).
Syntax for using byteValueExact() method: public byte
byteValueExact()
byte_VariableName = bigdecimal_objectName.byteValueExact();
In bigdecimal to byte transformation, sometimes the bigdecimal values needs rounding, as byte can hold only non-decimal values. This is necessary because as mentioned above, method throws Arithmetic Exception while
converting a bigdecimal value that has a non-zero fractional part.
Java_BigDecimal_byteValueExact.java
import java.math.BigDecimal;
|