Eng Kheng
Eng Kheng

Reputation: 5

Formatting the decimal place without converting the class numeric to character

I have a number, say 0.4 which is of class numeric. I wish to make it 2 decimal places without changing the class numeric. Is it possible to do this?

Upvotes: 0

Views: 113

Answers (1)

Ian Campbell
Ian Campbell

Reputation: 24790

I'm not totally sure what you mean, but you can control the number of digits that print with sprintf.

sprintf("%.2f",0.4)
# [1] "0.40"

Programmatically, 0.4 and 0.40 are identical.

all.equal(0.4,0.40)
# [1] TRUE

Upvotes: 1

Related Questions