Sean G Sean G - 1 month ago 8
R Question

How can I force R to display 2 decimal points in a large double?

I'm trying to sum a vector of doubles in a data frame. When the sum is relatively low, this works as intended.

df <- data.frame(
numbers = c(50, 632.5, 12.45)
)

sum(df$numbers)
# 694.95


But when the sum gets higher, the R begins to round the sum.

df <- data.frame(
numbers = c(50000000, 632.5, 12.45)
)
sum(df$numbers)
# 50000645


How can I stop R from eliminating these decimal points? The output I want is:

sum(df$numbers)
# 50000645.95

Answer

Try setting the number of digits in options, you can set whatever works for you. For example:

options(digits = 10)
sum(df$numbers)
[1] 50000644.95
Comments