round : Java Glossary


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

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:

This page is posted
on the web at:

Optional Replicator mirror
on local hard disk J:

Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.
no blog for this page
Your face IP:[]
You are visitor number