title: Format numbers to significant figures nicely in R
body: <p>I want to format numbers in my reports to <a href="http://en.wikipedia.org/wiki/Significant_digits" rel="noreferrer">significant digits</a>, but keep trailing significant zeroes and correctly format large numbers</p>
  
  <p>For instance the numbers c(10.00001,12345,1234.5,123.45,1.2345,0.12345) to 3 significant digits should be 10.0, 12300, 1230, 123,  1.23, 0.123 but I get differing results with different methods (and none seem to work universaly.</p>
  
  <pre><code>&gt; numbers&lt;-c(10.00001,12345,1234.5,123.45,1.2345,0.12345)
  &gt; for(n in seq(numbers)){
  +   print(signif(numbers[n],digits=3))
  +   print(format(numbers[n],digits=3))
  +   print(formatC(numbers[n], digits=3,format="fg"))
  +   print(formatC(numbers[n], digits=3,format="fg", flag="#"))
  +   }
  [1] 10
  [1] "10"
  [1] "  10"
  [1] "10.0"
  [1] 12300
  [1] "12345"
  [1] "12345"
  [1] "12345."
  [1] 1230
  [1] "1234"
  [1] "1234"
  [1] "1234."
  [1] 123
  [1] "123"
  [1] " 123"
  [1] "123."
  [1] 12.3
  [1] "12.3"
  [1] "12.3"
  [1] "12.3"
  [1] 1.23
  [1] "1.23"
  [1] "1.23"
  [1] "1.23"
  [1] 0.123
  [1] "0.123"
  [1] "0.123"
  [1] "0.123"
  </code></pre>
  
  <p>Here, signif and format round the 10.00001 result.  formatC with flag="#" correctly does the small numbers but not the large numbers.</p>
  
  <p>Is there a better way ?</p>