Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert int to BigDecimal with decimal - Java

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.

like image 808
cakes88 Avatar asked Mar 02 '16 21:03

cakes88


People also ask

How do I cast a float to BigDecimal?

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.

Does BigDecimal have decimal places?

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.

How does BigDecimal get integer value?

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.


1 Answers

You haven't been very specific about the semantics you want, but it looks like

BigDecimal.valueOf(i).movePointLeft(2)

does what you want.

like image 130
Louis Wasserman Avatar answered Sep 20 '22 12:09

Louis Wasserman