Is there a way to use DecimalFormat (or some other standard formatter) to format numbers like this:
1,000,000 => 1.00M
1,234,567 =>
Here's a subclass of NumberFormat that I whipped up. It looks like it does the job but I'm not entirely sure it's the best way:
private static final NumberFormat MILLIONS = new NumberFormat()
{
private NumberFormat LOCAL_REAL = new DecimalFormat("#,##0.00M");
public StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition pos)
{
double millions = number / 1000000D;
if(millions > 0.1) LOCAL_REAL.format(millions, toAppendTo, pos);
return toAppendTo;
}
public StringBuffer format(long number, StringBuffer toAppendTo, FieldPosition pos)
{
return format((double) number, toAppendTo, pos);
}
public Number parse(String source, ParsePosition parsePosition)
{
throw new UnsupportedOperationException("Not implemented...");
}
};