# How to round numbers (part 1)

Let's say you want to round a number to the nearest whole number because decimal values are not significant to you. Or, you want to round a number to multiples of 10 to simplify an approximation of amounts. There are several ways to round a number.

1. Select the cells that you want to format.

2. To display more or fewer digits after the decimal point, on the Home tab, in the Number group, click Increase Decimal or Decrease Decimal .

1. On the Home tab, in the Number group, click the arrow next to the list of number formats, and then click More Number Formats.

2. In the Category list, depending on the data type of your numbers, click Currency, Accounting, Percentage, or Scientific.

3. In the Decimal places box, enter the number of decimal places that you want to display.

Use the ROUNDUP function. In some cases, you may want to use the EVEN and the ODD functions to round up to the nearest even or odd number.

Use the ROUNDDOWNfunction.

Use the ROUND function.

Use the ROUND function.

Significant digits are digits that contribute to the accuracy of a number.

The examples in this section use the ROUND, ROUNDUP, and ROUNDDOWN functions. They cover rounding methods for positive, negative, whole, and fractional numbers, but the examples shown represent only a very small list of possible scenarios.

The following list contains some general rules to keep in mind when you round numbers to significant digits. You can experiment with the rounding functions and substitute your own numbers and parameters to return the number of significant digits that you want.

• When rounding a negative number, that number is first converted to its absolute value (its value without the negative sign). The rounding operation then occurs, and then the negative sign is reapplied. Although this may seem to defy logic, it is the way rounding works. For example, using the ROUNDDOWN function to round -889 to two significant digits results in -880. First, -889 is converted to its absolute value of 889. Next, it is rounded down to two significant digits results (880). Finally, the negative sign is reapplied, for a result of -880.
• Using the ROUNDDOWN function on a positive number always rounds a number down, and ROUNDUP always rounds a number up.
• The ROUND function rounds a number containing a fraction as follows: If the fractional part is 0.5 or greater, the number is rounded up. If the fractional part is less than 0.5, the number is rounded down.
• The ROUND function rounds a whole number up or down by following a similar rule to that for fractional numbers; substituting multiples of 5 for 0.5.
• As a general rule, when you round a number that has no fractional part (a whole number), you subtract the length from the number of significant digits to which you want to round. For example, to round 2345678 down to 3 significant digits, you use the ROUNDDOWN function with the parameter -4, as follows: = ROUNDDOWN(2345678,-4). This rounds the number down to 2340000, with the “234” portion as the significant digits.

There may be times when you want to round to a multiple of a number that you specify. For example, suppose your company ships a product in crates of 18 items. You can use the MROUND function to find out how many crates you will need to ship 204 items. In this case, the answer is 12, because 204 divided by 18 is 11.333, and you will need to round up. The 12th crate will contain only 6 items.

There may also be times where you need to round a negative number to a negative multiple or a number that contains decimal places to a multiple that contains decimal places. You can also use the MROUND function in these cases.

## round

round ( float \$val [, int \$precision = 0 [, int \$mode = PHP_ROUND_HALF_UP ]] ) : float

Note: PHP doesn't handle strings like “12,300.2” correctly by default. See converting from strings.

val

The value to round.

precision

• The optional number of decimal digits to round to.
• If the precision is positive, the rounding will occur after the decimal point.
• If the precision is negative, the rounding will occur before the decimal point. If the absolute value of the precision is greater than or equal to the number of digits, the result of the rounding is equal to 0

mode

Use one of the following constants to specify the mode in which rounding occurs.

The value rounded to the given precision as a float.

Example #1 round() examples

The above example will output:

float(3)
float(4)
float(4)
float(4)
float(1.96)
float(1242000)
float(5.05)
float(5.06)

Example #2 How precision affects a float

The above example will output:

float(1346.21)
float(1346.2)
float(1346)
float(1350)
float(1300)
float(1000)
float(0)

Example #3 mode examples

## Round to at most 2 decimal places (only if necessary)

This question is complicated.

Suppose we have a function, roundTo2DP(num), that takes a float as an argument and returns a value rounded to 2 decimal places. What should each of these expressions evaluate to?

• roundTo2DP(0.014999999999999999)
• roundTo2DP(0.0150000000000000001)
• roundTo2DP(0.015)

The 'obvious' answer is that the first example should round to 0.01 (because it's closer to 0.01 than to 0.02) while the other two should round to 0.02 (because 0.0150000000000000001 is closer to 0.02 than to 0.01, and because 0.015 is exactly halfway between them and there is a mathematical convention that such numbers get rounded up).

The catch, which you may have guessed, is that roundTo2DP cannot possibly be implemented to give those obvious answers, because all three numbers passed to it are the same number.

IEEE 754 binary floating point numbers (the kind used by JavaScript) can't exactly represent most non-integer numbers, and so all three numeric literals above get rounded to a nearby valid floating point number.

This number, as it happens, is exactly

0.01499999999999999944488848768742172978818416595458984375

which is closer to 0.01 than to 0.02.

You can see that all three numbers are the same at your browser console, Node shell, or other JavaScript interpreter. Just compare them:

> 0.014999999999999999 === 0.0150000000000000001
true

So when I write m = 0.0150000000000000001, the exact value of m that I end up with is closer to 0.01 than it is to 0.02. And yet, if I convert m to a String…

> var m = 0.0150000000000000001;
> console.log(String(m));
0.015
> var m = 0.014999999999999999;
> console.log(String(m));
0.015

… I get 0.015, which should round to 0.02, and which is noticeably not the 56-decimal-place number I earlier said that all of these numbers were exactly equal to. So what dark magic is this?

The answer can be found in the ECMAScript specification, in section 7.1.12.1: ToString applied to the Number type. Here the rules for converting some Number m to a String are laid down. The key part is point 5, in which an integer s is generated whose digits will be used in the String representation of m:

let n, k, and s be integers such that k ≥ 1, 10k-1 ≤ s < 10k, the Number value for s × 10nk is m, and k is as small as possible. Note that k is the number of digits in the decimal representation of s, that s is not divisible by 10, and that the least significant digit of s is not necessarily uniquely determined by these criteria.

The key part here is the requirement that “k is as small as possible”.

What that requirement amounts to is a requirement that, given a Number m, the value of String(m) must have the least possible number of digits while still satisfying the requirement that Number(String(m)) === m.

Since we already know that 0.015 === 0.0150000000000000001, it's now clear why String(0.0150000000000000001) === '0.015' must be true.

Of course, none of this discussion has directly answered what roundTo2DP(m) should return. If m's exact value is 0.01499999999999999944488848768742172978818416595458984375, but its String representation is '0.015', then what is the correct answer – mathematically, practically, philosophically, or whatever – when we round it to two decimal places?

There is no single correct answer to this. It depends upon your use case. You probably want to respect the String representation and round upwards when:

## How to round Python values to whole numbers?

When our Python program works with numerical values, every so often we have turn values with a fractional component into whole (integer) numbers. But how to do so? And what options does Python have? Let's find out together.

### # Round numerical values up and down in Python

When we round values, we go from a numerical value with decimal places to a whole number. With this process we do lose some precision, but the rounded value is often much easier to read and interpret.

Python has three ways to turn a floating-point value into a whole (integer) number:

• The built-in round() function rounds values up and down.
• The math.floor() function rounds down to the next full integer.
• The math.ceil() function rounds up to the next full integer.

If you just want a string or script output with a whole number, then a Python format string can perform that task too. That way you also don't lose precision in the original value.

Let's see how these three approaches work.

### # Round values up and down: Python's round() function

To round floating-point values up and down we use Python's round() function (Lutz, 2013; Python Docs, n.d. a). There are two ways to use this function. The first option is to round values to a certain number of decimals. The other option turns a floating-point value into a whole number.

To do that latter, we call round() with one argument: the value to turn into an integer. For example:

round(5.4598)
# Returns: 5

The round() function rounds values of .5 towards an even integer (Python Docs, n.d. a). So .5 is round up for positive values and round down for negative values.

For instance, both round(0.5) and round(-0.5) return 0, while round(1.5) gives 2 and round(-1.5) gives -2. This Python behaviour is a bit different from how rounding usually goes.

When we give round() an integer, the function simply returns that whole number. There's no error in that case, and so we don't have to check if the function's argument has a fractional value. It does need to be a number though; string values are not allowed in round().

### # Example: round Python numbers to the nearest full integer

To see how the round() function works in practice, let's consider the following mini-program:

# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953 # Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE) # Output rounded values
print(“Value:”.ljust(15), “Rounded:”) print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)

Here we first make five variables with floating-point values. Some have a lot of decimal places and others just a few.

Then we do some rounding. For that we call the round() function and provide one argument: the value to round. We store the rounded integers in the variables roundA through roundE.

Next we output the results with the print() function. For each variable we display the original value (e.g., valueA) and its rounded result (roundA). With the ljust() string method we justify that first value to the left. That aligns the values for a prettier output.

Here's how the rounded values look:

Value: Rounded:
3.14159265359 3
1845.7409947 1846
-100.95 -101
9.55432 10
34.49953 34

• Round to a number of decimal places with round()

### # Round down to the next integer: Python's math.floor() function

The math.floor() function returns the floor value of its argument, which is the nearest integer less than or equal to that argument's value (Python Docs, n.d. b).

That sounds abstract, but is just another way of saying that math.floor() rounds down to the next whole number. So 7.8 becomes 7 and 5.4 is turned into 5. And since the function rounds down to a smaller value, -8.2 becomes -9.

Here's a quick example of the math.floor() function:

import math math.floor(12.75)
# Returns: 12

math.floor() only accepts one argument: the value to round down. With a small custom function we can also round down to a number of decimal places. See round down to a specific decimal amount for more.

### # Example: round values down to the next full integer

To explore how the math.floor() function works in practice, let's examine the following Python program:

import math # Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138 # Round values down to the nearest full integer
roundA = math.floor(valueA)
roundB = math.floor(valueB)
roundC = math.floor(valueC)
roundD = math.floor(valueD)
roundE = math.floor(valueE) # Print the results
print(valueA, “rounded =”, roundA)
print(valueB, “rounded =”, roundB)
print(valueC, “rounded =”, roundC)
print(valueD, “rounded =”, roundD)
print(valueE, “rounded =”, roundE)

We first import the math module. That makes it possible to use the math.floor() function. Then we make five variables, valueA through valueE. Each holds a floating-point value.

Next we round those values down. For that we call the math.floor() function on each variable. We store the outcome in new variables (roundA through roundE).

The program's third part outputs the variables with Python's print() function. Here each print() statement displays the original value and its rounded down version. Here's how that looks:

11.282985 rounded = 11
19.2545879 rounded = 19
0.50000001 rounded = 0
34.6403001 rounded = 34
-9.9121138 rounded = -10

• Round down to a certain number of decimal places with math.floor()

### # Round up to the next integer: Python's math.ceil() function

The math.ceil() function returns the ceiling of its argument, which is the nearest integer greater than or equal to that argument's value (Python Docs, n.d. b).

That's just a way of saying that math.ceil() rounds up to a whole number: 3.2 becomes 4 and 7.75 gets turned into 8. And because the function rounds up to a greater value, -9.8 becomes -9.

Here's a quick example of math.ceil():

import math math.ceil(12.45)
# Returns: 13

Here's a way to remember the difference between math.floor() and math.ceil(). As you know, each floating-point value lies between two consecutive integers. 12.45, for instance, is between 12 and 13.

Now the “ceiling” is the higher endpoint of this interval. So math.ceil() returns 13. The lower start point of that integer interval is called the “floor”. So math.floor() returns 12.

Python's math.ceil() function always rounds up to a full integer. But with a small custom function we can also round up to a number of decimal places. See round Python values to decimal places for how.

### # Example: round Python values up to whole numbers

Let's see how math.ceil() works in practice. This example program rounds several floating-point values up to a whole number:

import math # Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138 # Round values up to the nearest full integer
roundA = math.ceil(valueA)
roundB = math.ceil(valueB)
roundC = math.ceil(valueC)
roundD = math.ceil(valueD)
roundE = math.ceil(valueE) # Output the results
print(valueA, “rounded =”, roundA)
print(valueB, “rounded =”, roundB)
print(valueC, “rounded =”, roundC)
print(valueD, “rounded =”, roundD)
print(valueE, “rounded =”, roundE)

We first import the math module. That makes the math.ceil() function available. Then we make five different variables, named valueA through valueE. Each has a number with several decimal places.

The next part rounds those values up to a full integer. To make that happen we call the math.ceil() function on each variable. We put the values returns by that function in new variables, roundA through roundE.

The third code segment has the print() function output both the original and rounded value. Here's what that displays:

11.282985 rounded = 12
19.2545879 rounded = 20
0.50000001 rounded = 1
34.6403001 rounded = 35
-9.9121138 rounded = -9

• Round up to a certain number of decimal places with math.ceil()

### # Round all values in a Python list or array

Of course there are also situations where we have a bunch of values to round, rather than a single value. There are two main ways to do that: with a list comprehension or for loop. Let's see.

### # Round Python values with a list comprehension

When we have a sequence of floating-point values, one way to round them is with a list comprehension. That requires just a bit of code and runs efficiently.

Here's a mini-program that does just that:

import math # Some random values
values = [ 3.46410162, 6.70820393, 11.04536102, 15.29705854, 21.21320344, 31.4960315
] # Generate new lists with values rounded
valuesRounded = [round(number) for number in values]
valuesRoundUp = [math.ceil(number) for number in values]
valuesRoundDown = [math.floor(number) for number in values]

# Output data
print(“Original values:
“, values)
print(“Rounded:
“, valuesRounded)
print(“Rounded up to next integer:
“, valuesRoundUp)
print(“Rounded down to next integer:
“, valuesRoundDown)

First we import the math module. That makes the math.ceil() and math.floor() rounding functions available. Then we make a list named values, which holds several floating-point values.

To round those values to whole numbers we make three list comprehensions. The first one executes round() for each list value. The other two execute math.ceil() and math.floor() functions on the list values.

We generate the values that those functions use with an in-line for loop: for number in values. This takes one value from the values list at a time, and makes it available through the number variable.

Those list comprehensions generate new lists. We assign those to the valuesRounded, valuesRoundUp, and valuesRoundDown variables.

The last part of the program outputs the original list and the three rounded ones. Here's how that looks:

Original values:
[3.46410162, 6.70820393, 11.04536102, 15.29705854, 21.21320344, 31.4960315]
Rounded:
[3, 7, 11, 15, 21, 31]
Rounded up to next integer:
[4, 7, 12, 16, 22, 32]
Rounded down to next integer:
[3, 6, 11, 15, 21, 31]

In the above example we kept the original list. If you don't need to retain those values, you can also overwrite the original list with rounded values. Here's how a list comprehension does that:

# Round all numbers in the 'values' list,
# in place (to replace the original numbers)
values = [round(value) for value in values]

### # Round all values with Python's for loop

Of course we can also round list or array values with a regular for loop. This requires a bit more code than a list comprehension, but makes it easier to perform additional operations on each element. Plus a for loop is easier to read in complex situations.

Here's how a Python program rounds values inside a for loop:

import math # Some random values
values = [ 3.46410162, 6.70820393, 11.04536102, 15.29705854, 21.21320344, 31.4960315
] # Create the new lists
valuesRounded = []
valuesRoundUp = []
valuesRoundDown = [] # Populate new lists with rounded values
for number in values:
valuesRounded.append(round(number))
valuesRoundUp.append(math.ceil(number))
valuesRoundDown.append(math.floor(number))

# Output data
print(“Original values:
“, values)
print(“Rounded (standard):
“, valuesRounded)
print(“Rounded up to next integer:
“, valuesRoundUp)
print(“Rounded down to next integer:
“, valuesRoundDown)

We again first import the math module to be able to use math.ceil() and math.floor(). Then we make a list (values) with floating-point values.

Then we make three initially empty lists: valuesRounded, valuesRoundUp, and valuesRoundDown. These are going to hold our rounded values.

To fill those lists we make a Python for loop. This loop goes through all elements in the values list. During each loop cycle the number variable holds a single element from that list.

Inside the loop we call the append() method on each of the three new lists. That way we add a new element to them. The value we add each pass through the loop is the number variable rounded with round(), math.ceil(), and math.floor(). After this loop is done, each of those three lists has a rounded value from the original list.