How to Change the Decimal Separator of Decimalformat from Comma to Dot/Point

How to change the decimal separator of DecimalFormat from comma to dot/point?

You can change the separator either by setting a locale or using the DecimalFormatSymbols.

If you want the grouping separator to be a point, you can use an european locale:

NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);
DecimalFormat df = (DecimalFormat)nf;

Alternatively you can use the DecimalFormatSymbols class to change the symbols that appear in the formatted numbers produced by the format method. These symbols include the decimal separator, the grouping separator, the minus sign, and the percent sign, among others:

DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(currentLocale);
otherSymbols.setDecimalSeparator(',');
otherSymbols.setGroupingSeparator('.');
DecimalFormat df = new DecimalFormat(formatString, otherSymbols);

currentLocale can be obtained from Locale.getDefault() i.e.:

Locale currentLocale = Locale.getDefault();

Change dot to comma at DecimalFormat

You need to set the format symbols using an instance of DecimalFormatSymbols:

public void testDec() {
DecimalFormat df = new DecimalFormat("#.000");
DecimalFormatSymbols sym = DecimalFormatSymbols.getInstance();
sym.setDecimalSeparator(',');
df.setDecimalFormatSymbols(sym);
System.out.println(df.format(1234567/1000000.0));
}

Output is

1,235

Force point (.) as decimal separator in java

Use the overload of String.format which lets you specify the locale:

return String.format(Locale.ROOT, "%.2f", someDouble);

If you're only formatting a number - as you are here - then using NumberFormat would probably be more appropriate. But if you need the rest of the formatting capabilities of String.format, this should work fine.

DecimalFormat(0.0) returns data with a comma as separator instead of a point

You can (and you actually need to avoid localization) actively configure the DecimalFormat with more detail as follows:

public class Main {
public static void main(String[] args) throws Exception {
DecimalFormat df = new DecimalFormat("0.0");
DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols();
decimalFormatSymbols.setDecimalSeparator('.');
df.setDecimalFormatSymbols(decimalFormatSymbols);

System.out.println(df.format(10.4)); // prints 10,4 instead of 10.4
System.out.println(df.format(100.5)); // prints 100,5 instead of 100.5
System.out.println(df.format(3000.3));// prints 3000,3 instead of 3000.3
}
}

You can read more details in the reference documentation, where an important snippet can be read:

Special Pattern Characters

(...)

The characters listed here are used
in non-localized patterns. Localized patterns use the corresponding
characters taken from this formatter's DecimalFormatSymbols object
instead, and these characters lose their special status.

DecimalFormat uses comma separator even when the pattern specified is a dot

The cause is the Locale. By default, you are connected to the Locale on your system, which return a decimal separator as ,. Using DecimalFormat, to change, do the following:

DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.US);
DecimalFormat df = new DecimalFormat("0.00", symbols);
double monthlyVal = forecastReturnValue / months;
return Double.valueOf(df.format(monthlyVal));

IMHO, I'd recommend using BigDecimal for such use cases, as it is more accurate, less error prone, and handle easily usage of rounding, scaling and so on.

The equivalent could be:

BigDecimal monthlyVal = BigDecimal.valueOf(20.0).divide(BigDecimal.valueOf(12), 2, RoundingMode.CEILING); // 2 is the scale. RoundingMode is up to you
System.out.println(monthlyVal);

How to change the grouping separator (thousands) of DecimalFormat from comma/point to quote using patterns?

Can you use a subclass of DecimalFormat instead?
You could analyze the pattern in the subclass and extract the unusual grouping separator from the pattern. On a call of the subclass's format method, you can use this grouping separator with the original DecimalFormat as shown in your own example code. This is the result that is returned by the format method of the subclass.

DecimalFormat point and comma as decimalSeparator

When you check the Oracle tutorial you can find that:

  • for formatting it is possible to alter the formatting symbols, but
  • for parsing there is no such thing.

And that makes a lot of sense: you are simply asking "I want the user to be able all kind of inconsistent data; and code shall magically turn that into what the user meant".

One way to improve the user experience: instead of using a formatted text field; you could simply allow the user to enter strings. And then you write validators that allow for that "magic" to happen.

In other words: if you want that "4,59" and "4.59" should result as the same thing, then you need to write code for that. Which could be as simple as using indexOf() to figure if "." or "," is present in the incoming string (ONCE); and then try to use "formatted" parsing for each of the cases using different patterns. If you decide for that steep route into the abyss, start reading about InputVerifiers here for example.

But of course: that can turn out to be complicated; because as said; you are basically allowing the user to put in whatever data; and expect to guess what he means with that.

My personal two cent: put a lot of warnings/examples around that text field; and allow exactly one locale based kind of input. And slap users fingers when he violates that.



Related Topics



Leave a reply



Submit