I'm having a hard time figuring out how to convert this. Here is what I want:
int i = 5900;
BigDecimal bD = new BigDecimal(i);
I need bD to be 59.00 but I can't figure out how to get this result. All I've been able to get is 5900 as type BigDecimal. Any suggestion would help, thanks.
float f = 45.6f; BigDecimal bd = BigDecimal. valueOf(f); Quote from documentations: Note: This is generally the preferred way to convert a double (or float) into a BigDecimal, as the value returned is equal to that resulting from constructing a BigDecimal from the result of using Double.
Immutable, arbitrary-precision signed decimal numbers. A BigDecimal consists of an arbitrary precision integer unscaled value and a 32-bit integer scale. If zero or positive, the scale is the number of digits to the right of the decimal point.
intValue()converts this BigDecimal to an int. This conversion is analogous to the narrowing primitive conversion from double to short. Any fractional part of this BigDecimal will be discarded, and if the resulting "BigInteger" is too big to fit in an int, only the low-order 32 bits are returned.
You haven't been very specific about the semantics you want, but it looks like
BigDecimal.valueOf(i).movePointLeft(2)
does what you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With