1

I am hoping for below outcomes :

If Input => 25.69 - OutCome should be 25.6

If Input => -0.40 - OutCome should be -0.4

If Input => -0.004 - OutCome should be 0 or 0.0

To achive this I have tries as below :

current=25.69
Log.e(">>> ceil ",">>> "+Math.ceil(current))
Log.e(">>> round ",">>> "+Math.round(current))
Log.e(">>> floor ",">>> "+Math.floor(current))

But getting out come as :

ceil: >>> 26.0 round: >>> 26 floor: >>> 25.0

How can I get 25.6 ?

Jaimin Modi
  • 1,530
  • 4
  • 20
  • 72

1 Answers1

1

I recommend using BigDecimal for complex rounding behaviors:

double current = 25.69;
double truncatedDouble = BigDecimal.valueOf(current).setScale(1, RoundingMode.DOWN).doubleValue();
System.out.println(truncatedDouble);

prints

25.6
Commander Tvis
  • 2,244
  • 2
  • 15
  • 41