3

The user can specify a file output format at the GUI. Especially he can define a number format. The first idea was to let him specify a format which can be used with DecimalFormat, but DecimalFormat has at least two main disadvantages for my usecase:

  • It uses the locale of the user, so ##.## converts the '.' to ',' in germany.
  • There is no difference between before comma and after comma numbers, so you cannot format 12.50 to 1250. Also required in my usecase.

StringFormat cannot do this either. So my question is if you know any library which already handles this problem, or do i have to program a simple formatter myself.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Dominik
  • 406
  • 2
  • 12
  • DecimalFormat takes the locale of the user if you don't set it using [DecimalFormatSymbols](http://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormatSymbols.html). – Denys Séguret Apr 17 '13 at 14:27
  • Check out the answer to this question: http://stackoverflow.com/questions/12711493/custom-formatted-number-with-period-and-comma – Jeremy G Apr 17 '13 at 14:28

2 Answers2

2
DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(Locale.ENGLISH);
DecimalFormat decimalFormat = new DecimalFormat("##.##");
decimalFormat.setDecimalFormatSymbols(otherSymbols);
xlm
  • 6,854
  • 14
  • 53
  • 55
Vinil Chandran
  • 1,563
  • 1
  • 19
  • 33
-1

It uses the locale of the user

Not so. From the Javadoc:

"To obtain a NumberFormat for a specific locale, including the default locale, call one of NumberFormat's factory methods, such as getInstance(). In general, do not call the DecimalFormat constructors directly".

user207421
  • 305,947
  • 44
  • 307
  • 483