How to round down if there is multiple 5 in the decimal places?

Viewed 32

I want to round a number down to two decimal places but if the number entered has half or has multiples of 5 it would always be rounded down.

For Example.

1.555555555 should be 1.55

1.555555556 should be 1.56

1.555555551 should be 1.55 etc,

Rounding.HALF_DOWN rounds 1.5555555 to 1.56 which is not what i want

it also rounds 1.55555551 to 1.56 but i want it as 1.55.

1 Answers

There are different ways how to perform rounding. See this Wikipedia page - and if it is not listed it does not mean it is invalid or not meaningful.

So you will have to either pick one of the functions provided by Java or provide that function yourself, be it in source or as third party library. Check the methods of java.lang.Math or java.lang.StrictMath.

Related