Is there a nice printing API for a math string somewhere?

Is there a nice print API for a math string somewhere?

For instance:

"10000000 - 234564"

becomes

"10,000,000 minus 234,564"

I can make my own, but can anyone give me some pointers on how to do the "," part?

+2


a source to share


4 answers


You can do it with DecimalFormat :



NumberFormat f = NumberFormat.getInstance(loc);
if (f instanceof DecimalFormat) {
  DecimalFormat df = (DecimalFormat) f;
  df.setGroupingUsed(true);
  df.setGroupingSize(3);
  System.out.println(df.format(myNumber));
}

      

+3


a source


When it comes to formatting "," (I think you mean the thousands separator). NumberFormat is your friend. More specifically, I think you would like to use your own DecimalFormat subclass , which can format a number according to a given pattern.



0


a source


The "," part can be performed with the "," Formatter flag used, for example. String.Format. It was called the Grouping Separator .

0


a source


Using

System.out.format("%," n);

      

Where n

is your extracted int. For more parameters (for example, using a class DecimalFormat

) read here .

0


a source







All Articles