title: How can I reduce the number of significant digits of any number in R?
body: <p>I'm looking for a quick and simple way to reduce the precision of a number in R, either for display on a chart or for inclusion in a report (text or table).</p>
  
  <p>My go-to function is <code>sprintf()</code>, but it doesn't appear that any of the format specifiers are sufficiently general to handle all possible cases, even though what I'm looking for is fairly common (it's what we are all taught in school).</p>
  
  <p>For example, if I want to round to 3 significant digits, I'm looking for a formula that could take:</p>
  
  <ul>
  <li>1,239,451 and return 1,240,000</li>
  <li>-12.1257 and return -12.1</li>
  <li>.0681 and return .0681</li>
  <li>5 and return 5</li>
  </ul>
  
  <p>Note: <a href="https://stackoverflow.com/a/1581007/1009730">This answer</a> provides what I believe is a javascript implementation of just such a function, but I'm no javascript expert, and the syntax of some of the functions used is not obvious to me. Also, I'm curious if I've missed some built-in functionality of base R or a common library to do just this.</p>