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 ?

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.