title: Is there a Java number formatting library that handles significant digits?
body: <p>The built in DecimalFormat only allows you to specify number of digits to the right of the decimal place.</p>
  
  <p>Because of the limitations of representing numbers as double, number formatting needs to include some level of rounding inside of it. In the general case, that rounding has to be to a number of significant digits (default case is the precision of a double) or else your formatted double will end up showing stuff like 3.5999999 instead of 3.6.</p>
  
  <p>The closest solution I could find is using</p>
  
  <pre><code>new BigDecimal(double, new MathContext(14, RoundingMode.HALF_EVEN).stripTrailingZeros().toPlainString()
  </code></pre>
  
  <p>however, that only provides a single format. If I need to format it generally (group separator, decimal separator, limit to number of digits, padding, etc.), there is nothing in the JDK library.</p>
  
  <p>Is there a general number formatting library out there that will handle rounding to significant digits properly?</p>
  
  <p>Someone asked for examples. So, let's say I wanted to format to 4 significant digits, then:</p>
  
  <pre><code>0.000003599999 -&gt; 0.0000036
  4.12345 -&gt; 4.123
  1234.345 -&gt; 1234
  </code></pre>
  
  <p>The general approach we would take would be to round to 14 digits since depending on the circumstances, a double can only represent around 15-17 significant digits anyway (or so I've read).</p>