Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

round

Round to nearest decimal or integer

Syntax

Description

example

Y = round(X) rounds each element of X to the nearest integer. In the case of a tie, where an element has a fractional part of exactly 0.5, the round function rounds away from zero to the integer with larger magnitude.

example

Y = round(X,N) rounds to N digits:

  • N > 0: round to N digits to the right of the decimal point.

  • N = 0: round to the nearest integer.

  • N < 0: round to N digits to the left of the decimal point.

example

Y = round(X,N,type) specifies the type of rounding. Specify 'significant' to round to N significant digits (counted from the leftmost digit). In this case, N must be a positive integer.

example

Y = round(t) rounds each element of the duration array t to the nearest number of seconds.

example

Y = round(t,unit) rounds each element of t to the nearest number of the specified unit of time.

Examples

collapse all

Round the elements of a 2-by-2 matrix to the nearest integer.

X = [2.11 3.5; -3.5 0.78];
Y = round(X)
Y =

     2     4
    -4     1

Round pi to the nearest 3 decimal digits.

Y = round(pi,3)
Y =

    3.1420

Round the number 863178137 to the nearest multiple of 100.

round(863178137,-2)
ans =

   863178100

Round the elements of a vector to retain 2 significant digits.

format shortg
x = [1253 1.345 120.44]
y = round(x,2,'significant')
x =

         1253        1.345       120.44


y =

         1300          1.3          120

The format command controls how MATLAB® displays numbers at the command line. If a number has extra digits that cannot be displayed in the current format, then MATLAB automatically rounds the number for display purposes. This can lead to unexpected results when combined with the round function.

Consider the result of the following subtraction operation, which displays 5 digits.

format short
x = 112.05 - 110
x =

    2.0500

Based on the displayed value of x, rounding x to 1 decimal should return 2.1.

round(x,1)
ans =

     2

In fact, the problem here is that MATLAB is rounding x to 5 digits for display purposes. The round function returns the correct answer. Confirm the answer by viewing x with format long, which displays x rounded to 15 digits.

format long
x
x =

   2.049999999999997

Round each value in a duration array to the nearest number of seconds.

t = hours(8) + minutes(29:31) + seconds(1.3:0.5:2.3);
t.Format = 'hh:mm:ss.SS'
t = 

  1×3 duration array

   08:29:01.30   08:30:01.80   08:31:02.30

Y1 = round(t)
Y1 = 

  1×3 duration array

   08:29:01.00   08:30:02.00   08:31:02.00

Round each value in t to the nearest number of hours.

Y2 = round(t,'hours')
Y2 = 

  1×3 duration array

   08:00:00.00   09:00:00.00   09:00:00.00

Input Arguments

collapse all

Input array, specified as a scalar, vector, matrix, or multidimensional array. For complex X, round treats the real and imaginary parts independently.

X must be single or double when you use round with more than one input.

round converts logical and char elements of X into double values.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | char | logical
Complex Number Support: Yes

Number of digits, specified as a scalar integer. When you specify N, the round function rounds X to the nearest multiple of 10 -N.

If you specify the 'significant' rounding type, then N must be a positive integer.

Rounding type, specified as 'decimals' or 'significant'. The rounding type determines whether round considers digits in relation to the decimal point or the overall number of significant digits. N must be a positive integer when you specify 'significant'. In that case, the round function rounds to the nearest number with N significant digits.

The default value is 'decimals', so that round(X,N,'decimals') is equivalent to round(X,N).

Example: round(3132,2,'significant') returns 3100, which is the closest number to 3132 that has 2 significant digits.

Data Types: char

Input duration, specified as a duration array.

Unit of time, specified as 'seconds', 'minutes', 'hours', 'days', or 'years'. A duration of 1 year is equal to exactly 365.2425 24-hour days.

More About

collapse all

Compatibility Considerations

In R2014b, these syntaxes were added to round to any number of decimal or significant digits and to round duration values:

Y = round(X,N)
Y = round(X,N,type)
Y = round(t)  
Y = round(t,unit)   

Older versions support only this syntax, which rounds to the nearest integer:

Y = round(X) 

Tall Array Support

This function fully supports tall arrays. For more information, see Tall Arrays.

Tips

  • format short and format long both display rounded numbers. This can cause unexpected results when combined with the round function.

  • For display purposes, use sprintf to control the exact display of a number as a string. For example, to display exactly 2 decimal digits of pi (and no trailing zeros), use sprintf('%.2f',pi).

See Also

| |

Introduced before R2006a

Was this topic helpful?