Ben
Ben

Reputation: 21625

How do I format a number in thousands in R

Suppose I have a number like 123542.52. How do I display that number to the nearest thousand? In this example, 124 should be displayed.

Upvotes: 0

Views: 3376

Answers (3)

Thomas
Thomas

Reputation: 44525

Just use a negative digits argument in round():

round(123542.52, -3)/1000
# [1] 124

As described in ? round:

Rounding to a negative number of digits means rounding to a power of ten, so for example round(x, digits = -2) rounds to the nearest hundred.

Upvotes: 5

Mayou
Mayou

Reputation: 8818

You could do this:

 x = 123542.52
 y = signif(x,3)
 y <- as.numeric(gsub("0","",y))

Upvotes: 0

Doctor Dan
Doctor Dan

Reputation: 771

How's sprintf("%.0f", 123542.52/1000) ?

Upvotes: 1

Related Questions