round

Rounds to the nearest integer, or to a given number of digits.

Category: Language Fundamentals > Operators and Elementary Operations

Syntax

Y = round(X)

Rounds each element to the nearest integer. Halves round away from zero.

Y = round(X, N)

Rounds to N digits after the decimal point. A negative N rounds to tens, hundreds, and so on.

Y = round(X, N, 'significant')

Rounds to N significant digits.

Description

A value exactly halfway between two integers goes to the one farther from zero: round(2.5) is 3 and round(-2.5) is -3. This differs from the banker's rule used by some languages.

The digits form uses scaling by powers of ten, so results such as round(2.675, 2) follow the stored binary value, not the decimal text.

Input arguments

X
Numeric array. Complex values have both parts rounded.
N
Integer number of digits.

Output arguments

Y
Rounded values, same size and class as X.

Example

round([2.4 2.5 -2.5 3.6])
round(pi, 3)
round(1234, -2)
round(0.0012345, 2, 'significant')
Output
ans = 1×4

     2     3    -3     4
ans = 3.1420
ans = 1200
ans = 0

See also

floor ceil fix mod