Rounding to the nearest integer

Use only in the MuPAD Notebook Interface.

This functionality does not run in MATLAB.


round(x, <n>)


round rounds a number to the nearest integer.

For complex arguments, round rounds the real and the imaginary parts separately.

For arguments that contain symbolic identifiers, round returns unevaluated function calls.

For floating-point intervals, round returns floating-point intervals containing all the results of applying round to the real or complex numbers inside the interval.

round(x,n) returns a floating-point number with the rounded n-th decimal digit after the decimal point and sets all further digits to zero. If n is a negative integer, then round rounds the corresponding digit to the left of the decimal point. See Example 2.

    Note:   If the argument is a floating-point number of absolute value larger than 10DIGITS, the resulting integer is affected by internal non-significant digits. See Example 3.

    Note:   Internally, exact numerical expressions that are neither integers nor rational numbers are approximated by floating-point numbers before rounding. Thus, the resulting integer depends on the current DIGITSsetting. See Example 4.

Environment Interactions

The functions are sensitive to the environment variable DIGITS which determines the numerical working precision.


Example 1

Round the following real and complex numbers:

round(3.5), round(-7/2), round(I/2)

Round the following symbolic expression representing a number:

round(PI*I + 7*sin(exp(2)))

Rounding of expressions with symbolic identifiers produces unevaluated function calls:

round(x + 1)

Example 2

round(x, n) rounds the n-th decimal digit of the floating-point representation of x:

round(123.456, 1), round(123.456, 2), round(123.456, 3),
round(123.456, 4), round(123.456, 5)

float(exp(5)*PI), round(exp(5)*PI, 3)

If second argument is a negative integer, round rounds the digits to the left of the decimal point:

round(123.45, 1), round(123.45, 0), round(123.45, -1),
round(123.45, -2), round(123.45, -3)

Example 3

Rounding floating-point numbers of large absolute value is affected by internal non-significant digits:

x := 10^30/3.0

Note that only the first 10 decimal digits are "significant". Further digits are subject to round-off effects caused by the internal binary representation. These "insignificant" digits are part of the integer produced by rounding:


delete x:

Example 4

Exact numerical expressions are internally converted to floating-point numbers before rounding. Consequently, the current setting of DIGITS can affect the result:

x := 10^30 - exp(30)^ln(10)

Note that the exact value of this number is 0. Floating-point evaluation is subject to severe cancellations:

DIGITS := 10:
float(x), round(x)

The floating-point result is more accurate when calculated with a higher precision. The rounded values change accordingly:

DIGITS := 20:
float(x), round(x)

DIGITS := 30:
float(x), round(x)

delete x, DIGITS:

Example 5

On floating-point intervals, round behaves as follows:


This interval contains the results of round(x) for all .

Because there are finite numbers represented as RD_INF and RD_NINF, respectively, round returns very small or large representable numbers in certain cases:




Arithmetical expression or floating-point interval


Integer. If n is positive, the n-th digit after the decimal point is rounded. If n is negative, the |n|-th digit before the decimal point is rounded.

Return Values

Arithmetical expression.

Overloaded By


See Also

MuPAD Functions

Was this topic helpful?