rgtsignificant-digits

Display trailing zeros with signif


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.


Solution

  • Just format the values using sprintf before printing them:

    sprintf('%#.2g', c(0.301, 0.000502))
    [1] "0.30"    "0.00050"