Home > Back-end >  Math Function to convert Digits in Android - Kotlin
Math Function to convert Digits in Android - Kotlin

Time:09-18

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 ?

CodePudding user response:

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
  • Related