Round to nearest decimal or integer (2024)

Table of Contents
Syntax Description Examples Round Matrix Elements Round to Specified Number of Decimal Digits Round to Nearest Multiple of 100 Round Elements to Specified Number of Significant Digits Control Number Display While Rounding Specify Rounding Direction for Ties Round Duration Values Input Arguments X — Input array scalar | vector | matrix | multidimensional array | table | timetable N — Number of digits scalar integer type — Rounding type "decimals" (default) | "significant" direction — Direction to break ties "fromzero" (default) | "tozero" | "even" | "odd" | "plusinf" | "minusinf" t — Input duration duration array unit — Unit of time "seconds" (default) | "minutes" | "hours" | "days" | "years" Tips Extended Capabilities Tall Arrays Calculate with arrays that have more rows than fit in memory. C/C++ Code Generation Generate C and C++ code using MATLAB® Coder™. GPU Code Generation Generate CUDA® code for NVIDIA® GPUs using GPU Coder™. Thread-Based Environment Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool. GPU Arrays Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™. Distributed ArraysPartition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™. Version History R2023a: Perform calculations directly on tables and timetables R2022a: Control tiebreak behavior R2022a: round returns consistent results for ties R2014b: Rounding to specified number of digits See Also Topics MATLAB Command Americas Europe Asia Pacific References

Round to nearest decimal or integer

collapse all in page

Syntax

Y = round(X)

Y = round(X,N)

Y = round(X,N,type)

Y = round(___,TieBreaker=direction)

Y = round(t)

Y = round(t,unit)

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 0.5 (within roundoff error) in decimal, the round function rounds away from zero to the nearest 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(___,TieBreaker=direction) rounds ties as specified by direction. Use this argument after any of the input argument combinations in the previous syntaxes.

example

Y = round(t) roundseach element of the duration array t tothe nearest number of seconds.

example

Y = round(t,unit) roundseach element of t to the nearest number of thespecified unit of time.

Examples

collapse all

Round Matrix Elements

Open Live Script

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×2 2 4 -4 1

Round to Specified Number of Decimal Digits

Open Live Script

Round pi to the nearest 3 decimal digits.

Y = round(pi,3)
Y = 3.1420

Round to Nearest Multiple of 100

Open Live Script

Round the number 863178137 to the nearest multiple of 100.

round(863178137,-2)
ans = 863178100

Round Elements to Specified Number of Significant Digits

Open Live Script

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

X = [1253 1.345 120.44]
X = 1×3103 × 1.2530 0.0013 0.1204
Y = round(X,2,"significant")
Y = 1×3103 × 1.3000 0.0013 0.1200

Control Number Display While Rounding

Open Live Script

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 display can lead to unexpected results when combined with the round function.

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

format shortx = 112.05 - 110
x = 2.0500

The displayed result is 2.0500, which looks like a tie. However, due to the floating-point arithmetic error, the tie at a fractional part of 0.5 is not within roundoff error.

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

y = round(x,1)
y = 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 longx
x = 2.049999999999997

For comparison, show the rounding results for a tie that is within roundoff error and for a tie that is not within roundoff error.

x1 = 2.05
x1 = 2.050000000000000
y1 = round(x1,1)
y1 = 2.100000000000000
x2 = 2.05 - eps(2.05)
x2 = 2.049999999999999
y2 = round(x2,1)
y2 = 2

Specify Rounding Direction for Ties

Open Live Script

Create a vector of decimals that have ties, that is, decimals with a fractional part of 0.5 (within roundoff error).

X = -2.5:1:2.5
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 Values

Open Live Script

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 = 1x3 duration 08:29:01.30 08:30:01.80 08:31:02.30
Y1 = round(t)
Y1 = 1x3 duration 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 = 1x3 duration 08:00:00.00 09:00:00.00 09:00:00.00

Input Arguments

collapse all

XInput array
scalar | vector | matrix | multidimensional array | table | timetable

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

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

round converts logical and char elementsof X into double values.

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

NNumber of digits
scalar integer

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

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

typeRounding type
"decimals" (default) | "significant"

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 | string

directionDirection to break ties
"fromzero" (default) | "tozero" | "even" | "odd" | "plusinf" | "minusinf"

Direction to break ties, specified as one of these values:

  • "fromzero" — Round ties away from zero to the nearest integer with larger magnitude.

  • "tozero" — Round ties towards zero to the nearest integer with smaller magnitude.

  • "even" — Round ties to the nearest even integer.

  • "odd" — Round ties to the nearest odd integer.

  • "plusinf" — Round ties towards positive infinity to the nearest integer with larger value.

  • "minusinf" — Round ties towards negative infinity to the nearest integer with smaller value.

Ties are rare. When using round(X,N,TieBreaker=direction), a tie occurs only when X*10N is within roundoff error of a point halfway between two consecutive integers, that is, X*10N has a fractional part of 0.5 (within roundoff error) in decimal.

Example: round(2.015,2,TieBreaker="even")

tInput duration
duration array

Input duration, specified as a duration array.

unitUnit of time
"seconds" (default) | "minutes" | "hours" | "days" | "years"

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.

Data Types: char | string

Tips

  • format short and format long both display rounded numbers. This display 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).

Extended Capabilities

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

Version History

Introduced before R2006a

expand all

The round function can calculate on all variables within a table or timetable without indexing to access those variables. All variables must have data types that support the calculation. For more information, see Direct Calculations on Tables and Timetables.

Specify how to break ties by using the TieBreaker name-value argument. For example, round(X,TieBreaker="tozero") rounds ties towards zero.

See Also

ceil | fix | floor

Topics

  • Integers
  • Floating-Point Numbers

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

 

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Round to nearest decimal or integer (1)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

Contact your local office

Round to nearest decimal or integer (2024)

References

Top Articles
Latest Posts
Article information

Author: Jonah Leffler

Last Updated:

Views: 5794

Rating: 4.4 / 5 (45 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Jonah Leffler

Birthday: 1997-10-27

Address: 8987 Kieth Ports, Luettgenland, CT 54657-9808

Phone: +2611128251586

Job: Mining Supervisor

Hobby: Worldbuilding, Electronics, Amateur radio, Skiing, Cycling, Jogging, Taxidermy

Introduction: My name is Jonah Leffler, I am a determined, faithful, outstanding, inexpensive, cheerful, determined, smiling person who loves writing and wants to share my knowledge and understanding with you.