Round to nearest decimal or integer Show
SyntaxDescriptionexample
example
example
example
example
example
Examplescollapse all Round Matrix ElementsRound the elements of a 2-by-2 matrix to the nearest integer. X = [2.11 3.5; -3.5 0.78]; Y = round(X) Round to Specified Number of Decimal DigitsRound Round to Nearest Multiple of 100Round the number Round Elements to Specified Number of Significant DigitsRound the elements of a vector to retain 2 significant digits. X = 1×3
103 ×
1.2530 0.0013 0.1204
Y = round(X,2,"significant") Y = 1×3
103 ×
1.3000 0.0013 0.1200
Control Number Display While RoundingThe Consider the result of this subtraction operation, which displays 5 digits. format short
x = 112.05 - 110 The displayed result is Based on the displayed value of In fact, the problem here is that MATLAB is rounding For comparison, show the rounding results for a tie that is within roundoff error and for a tie that is not within roundoff error. Specify Rounding Direction for TiesCreate a vector of decimals that have ties, that is, decimals with a fractional part of X = 1×6
-2.5000 -1.5000 -0.5000 0.5000 1.5000 2.5000
Round the ties to the nearest even and odd integers. Yeven = round(X,TieBreaker="even") Yeven = 1×6
-2 -2 0 0 2 2
Yodd = round(X,TieBreaker="odd") Yodd = 1×6
-3 -1 -1 1 1 3
Round the ties towards positive and negative infinity. Yplusinf = round(X,TieBreaker="plusinf") Yplusinf = 1×6
-2 -1 0 1 2 3
Yminusinf = round(X,TieBreaker="minusinf") Yminusinf = 1×6
-3 -2 -1 0 1 2
Round the ties away from zero and towards zero. Yfromzero = round(X,TieBreaker="fromzero") Yfromzero = 1×6
-3 -2 -1 1 2 3
Ytozero = round(X,TieBreaker="tozero") Ytozero = 1×6
-2 -1 0 0 1 2
Round Duration ValuesRound 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 = 1x3 duration
08:29:01.30 08:30:01.80 08:31:02.30
Y1 = 1x3 duration
08:29:01.00 08:30:02.00 08:31:02.00
Round each value in Y2 = 1x3 duration
08:00:00.00 09:00:00.00 09:00:00.00
Input Argumentscollapse all X — Input array scalar | vector | matrix | multidimensional arrayInput array, specified as a scalar, vector, matrix, or multidimensional array. For complex
Data Types: N — Number of digits scalar integerNumber of digits, specified as a scalar integer. When you specify If you specify the type — Rounding type "decimals" (default) | "significant"Rounding type, specified as The default value is Example: Data
Types: direction — Direction to break ties "fromzero" (default) | "tozero" | "even" | "odd" | "plusinf" | "minusinf"Direction to break ties, specified as one of these values:
Ties are rare. When using Example: t — Input duration duration arrayInput duration, specified as a unit — Unit of time "seconds" (default) | "minutes" | "hours" | "days" | "years"Unit of time, specified as Data Types: Tips
Extended CapabilitiesTall Arrays Calculate with arrays that have more rows than fit in memory.This function fully supports tall arrays. For more information, see Tall Arrays. C/C++ Code Generation Generate C and C++ code using MATLAB® Coder™.Usage notes and limitations:
GPU Code Generation Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.Usage notes and limitations:
Thread-Based Environment Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment. GPU Arrays Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.Usage notes and limitations:
For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox). Distributed Arrays Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox). Version HistoryIntroduced before R2006a expand all R2022a: Control tiebreak behaviorSpecify how to break ties by using the R2022a: round returns consistent results for tiesStarting in R2022a, the X = 1.015:5.015; N = 2; Y = round(1.015:5.015,2) Y = 1.0200 2.0200 3.0200 4.0200 5.0200 In
previous releases, the R2014b: Rounding to specified number of digitsIn 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 of MATLAB® support only this syntax, which rounds to the nearest integer: |