I have a server returning a value (in JSON) which contains a number with decimal places, e.g. 203.14
.
Since in my app I need to do some calculation, I have to convert it to Double
.
However when it is converted to Double, it's value becomes 203.13999938964844
.
For some business logic reason, when I display this value, I need to round down this number to 2 decimal places.
So now it becomes 203.13
, which is 0.01 smaller than the value server returns.
How can I preserve 203.14
when I convert "203.14"
to Double?
Update:
I just found that the reason is because the model class used float
to accept the server value, but then later converted to Double
. If day one the model class uses double
to accept the value, then the problem above will not exist.