This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line! See this thread for details.

Thanks a lot. I am not too clear how to achieve what I want. DecimalFormat is more for formatting the decimal numbers. I am interested in rounding off. Can you please give an example using the API

Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140

posted

0

There is a method called setMaximumFractionDigits() in that class. That might be useful.

Originally posted by Vasim Patel: How do I round off the decimal in java to the nearest .05

Since 0.05 is a decimal number, and float/double are binary approximations of decimal numbers, you can't be guaranteed to round exactly using them. If you just want to get close, try this:In words, you shift the rounding scale to be 1 instead of 0.05, round the value to nearest integer, and shift back. If you need exact decimal values, use java.math.BigDecimal.