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
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.
Posted on: November 21, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles