round( )
Round to a fixed number of decimal places. round() is a base R function.
Required Library
# round() is part of base R — no package installation neededUse round() when a fixed number of decimal places is required — percentages reported to 2 d.p., pH to 2 d.p., temperatures to 1 d.p. Use signif() for analytical concentrations and masses where significant figures reflect instrument precision.
Syntax
round(x)round(x) rounds a number (or numeric vector) to a user-specified number of decimal places. If nothing is specified, the default is 0, meaning that the result has no decimal places.
R uses banker’s rounding: when a value is exactly halfway between two options, it rounds to the nearest even digit. round(0.5) gives 0, and round(1.5) gives 2.
Store all intermediate values at full precision. Call round() only inside cat() when reporting a final result. Never call round() inside mutate() on a column that will be used in subsequent calculations.
# ✓ Correct
pct_label <- ibu_mg / label_claim_mg * 100 # full precision stored
cat("Label claim:", round(pct_label, 2), "%\n") # round only when printing
# ✗ Avoid
mutate(pct_label = round(ibu_mg / label_claim_mg * 100, 2)) # rounding contaminates downstream calcsExamples
Comparing round() to the raw value
Positive vs. negative digits
Applying to a column or vector
signif() vs round() — which to use?
Argument Overview
Required arguments must be included when using a function while optional arguments can be included on demand.
x — number(s) to round numeric | integer Required
A single number or a numeric vector. round() is vectorised — it applies to every element of a vector.
Data Types: numeric | integer
digits — number of decimal places integer Optional
Number of digits to the right of the decimal point. Default is 0, which rounds to the nearest integer. Negative values round to the left of the decimal point.
round(x, digits = 0)round(x, digits = 0) adds the optional digits argument that controls how many decimal places to keep. The default 0 rounds to the nearest integer; negative values round to the left of the decimal point.
digits |
Meaning | Example: round(1587.346, digits) |
|---|---|---|
2 |
2 decimal places | 1587.35 |
1 |
1 decimal place | 1587.3 |
0 |
nearest integer (default) | 1587 |
-1 |
nearest 10 | 1590 |
-2 |
nearest 100 | 1600 |
Data Types: integer · Default: 0