round
Rounding Functions
Description
Returns an object of the same class and format as the input, with the
numbers either rounded or retaining a specified number of significant
digits.
Usage
round(x, digits = 0)
signif(x, digits = 6)
Arguments
x |
a numeric or complex object. Missing values (NAs) are allowed.
|
digits |
- In the case of round, an integer that specifies the number of digits
after the decimal point. digits can be negative for rounding large
numbers to the nearest 10, 100, and so on.
- In the case of signif, an integer that specifies the total number of
digits. If digits is negative or zero, it is treated as 1.
|
Details
These functions are in the Math group of generic functions. See Methods.
The two common conventions in an operation to round a five are:
- Go to the higher number.
- Go to the even digit.
Both
round and
signif adhere to the second convention.
Rounding is affected by floating point binary representation.
Therefore,
round(1.225,2) correctly gives the result
1.23
but
round(1.015,2) gives the result
1.01.
You can use options(digits=n) to view the representation of a number more clearly to help determine if rounding occurs in the expected direction for a given number.
Value
returns an object that is the same class and format of x with the
data rounded to the specified number of places (round) or with
the specified number of significant digits retained (signif).
Classes
This function is the default method for classes that do not inherit a specific method for the function and for the Math group of functions. The result retains the class and the attributes of the input. If this behavior is not appropriate, the designer of the class should provide a method for the function or for the Math group.
See Also
Examples
round(pi, dig=2) # round to 2 decimals
round(123, -1) # round to nearest 10
x <- c(123456, .123456, .000123456)
round(x, 3)
# [1] 123456.000 0.123 0.000
signif(x, 3)
# [1] 1.23e+05 1.23e-01 1.23e-04
round(c(-1.9, -1.1, 1.1, 1.9))
# [1] -2 -1 1 2