title: Java: Format number in millions
body: <p>Is there a way to use DecimalFormat (or some other standard formatter) to format numbers like this:</p>
  <blockquote>
  <p>1,000,000     =&gt; 1.00M</p>
  <p>1,234,567     =&gt; 1.23M</p>
  <p>1,234,567,890 =&gt; 1234.57M</p>
  </blockquote>
  <p>Basically dividing some number by 1 million, keeping 2 decimal places, and slapping an 'M' on the end.  I've thought about creating a new subclass of NumberFormat but it looks trickier than I imagined.</p>
  <p>I'm writing an API that has a format method that looks like this:</p>
  <pre><code>public String format(double value, Unit unit); // Unit is an enum
  </code></pre>
  <p>Internally, I'm mapping Unit objects to NumberFormatters.  The implementation is something like this:</p>
  <pre><code>public String format(double value, Unit unit)
  {
      NumberFormatter formatter = formatters.get(unit);
      return formatter.format(value);
  }
  </code></pre>
  <p>Note that because of this, I can't expect the client to divide by 1 million, and I can't just use String.format() without wrapping it in a NumberFormatter.</p>