Java compensate 0.05 for money

I have 7.125 (double) and need to make it 7.15 What is the easiest way?

Found round, but I got 7.13, please help

thank you

Solution

The simplest way is to multiply by 20.0, round to an integer and divide by 20.0 again Think of it this way: 1.0 / 20.0 = 0.05... That's why it works

Note that rounding is different from floor (or convert to int) You should use Java. Java for double and float types lang.Math. Round(), use Java. Java for BigDecimal type math. BigDecimal. round(MathContext)

You might want to use BigDecimal

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>