round : Java Glossary

round
To take a float or double and want the corresponding integer (possibly as a float, double, int or long) there are five basic algorithms you might use: round, ceil, floor, truncate and cover. In elementary school you learned that rounding always involves 0.5. Java and the IEEE (Institute of Electrical & Electronics Engineers) use the term in a more general sense to describe any way a number is converted from floating point to integer.

DecimalFormat automatically rounds output to the displayed number of decimal places, so there is nothing to do if you are rounding simply to make output look pretty.

Integer Division Rounding Gotcha
Round Rounding to a Multiple
Ceiling Rounding Currency
Floor Rounding to a Nickel
Truncate Links
Cover

Rounding Gotcha

Rounding to a Multiple

// rounding m up to next highest multiple of n
int ceil = ( m + n - 1 ) / n * n;

// rounding m down to multiple of n
int floor = m / n * n;

// rounding m to nearest multiple of n
int near = ( m + n/2 ) / n * n;

Rounding Currency

Rounding To The Nickel

On 2013-02-04 Canada discontinued the penny. Transactions are still computed to the penny, but are rounded to the nickel. The government has suggested (but not mandated) the following rounding rules.

The formula to compute the rounded amount could be described in words as.

  1. Start with the cost in pennies.
  2. add 2
  3. divide by 5, discarding the fraction.
  4. multiply by 5

You can calculate the rounded amount in a spreadsheet with =ROUNDDOWN((A1+2)/5)*5

The Canadian Sales Tax Calculator will compute the final sale amount including sales tax both penny and nickel-rounded.

Learning More

Oracle’s Javadoc on Math.ceil : available:
Oracle’s Javadoc on Math.floor : available:
Oracle’s Javadoc on Math.rint : available:
Oracle’s Javadoc on Math.round : available:

CMP homejump to top

available on the web at:

http://mindprod.com/jgloss/round.html
  

optional Replicator mirror
of mindprod.com
on local hard disk J:

J:\mindprod\jgloss\round.html
logo
Please email your , letters to the editor, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : feedback email. If you want your message, your name or email kept confidential, not considered for public posting, please explicitly specify that. Unless you state otherwise, I will treat your message as a letter to the editor that I may or may not publish in the feedback section. After that, it will be too late to retract it. If you disagree with something I said, especially when sending an ad-hominem attack, a rant composed mainly of obscenities or a death threat, please quote the offending passage and cite the web page where you found it, tell me why you think it is wrong, and, if possible, provide some supporting evidence. I can’t very well fix erroneous or ambiguous text if I can’t find it.
Blog
IP:[65.110.21.43]
Your face IP:[184.72.184.104]
You are visitor number 135,465.