Link to home
Start Free TrialLog in
Avatar of schavalam
schavalam

asked on

how to round the double value

if the number is 1.4567 then i need out is 1.46 so what to.
i am using java.lang.Math.round(1.4567) getting output is 1 but i want 1.46
please help me
Avatar of jimmack
jimmack

Multiply it by 100, round it then divide it by 100.
Hi schavalam:

use java.math.BigDecimal class. It is meant for this purpose only (currency fomatting, which as you know goes to 2 decimal points of precision like $23.75)

Taps


Avatar of CEHJ
>>getting output is 1 but i want 1.46

You must be assigning it to an integral variable such as int or long. Assign to a double or float
CEHJ:  Math.round() returns a long or an int, so can't be used to round a double.

Not like you to miss that :-(
You're right ;-)
ASKER CERTIFIED SOLUTION
Avatar of f_98
f_98

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Use the RoundUp/RoundDown option on BigDecimal.