1

I am creating tables displaying numerical data with the gt package in R. I need to display 2 significant figures in my table and want to show trailing zeros if relevant (for aesthetic purposes). For example, I need the number 0.301 to display as 0.30. The number 0.000502 to display as 0.00050. How do I do this? The function signif() displays these values as 0.3 and 0.0005.

Nicki_NZ
  • 123
  • 8

1 Answers1

4

Just format the values using sprintf before printing them:

sprintf('%#.2g', c(0.301, 0.000502))
[1] "0.30"    "0.00050"
Onyambu
  • 67,392
  • 3
  • 24
  • 53
  • 2
    Thanks for your comment, further to this, how would I get sprintf to not put a decimal place if the number is an integer? I want it to display 11 as "11" not "11." ```sprintf('%#.2g', 11)``` – Nicki_NZ Mar 15 '22 at 22:38