One Star

round to 2 dp with Float.ValueOf()

hi,
pls let me know any one...
round to 2 dp with Float.ValueOf()?

thx
1 REPLY
One Star

Re: round to 2 dp with Float.ValueOf()

Hi buzzy,
What is the input data type? Also, float is only 32bit, so depending on the size of your number, you might lose precision after you round. Double or BigDecimal may be a better choice.
For a double, you could use this to round: (Math.round(myField*100.0))/100.0
For a BigDecimal, you could use this to round: myField.setScale(2, BigDecimal.ROUND_HALF_UP)
Thanks,
Ben