My DecimalFormat.format() method changes my double's dot to a comma. Why would it do that?

Ok, so I did some debugging and now know what the problem is. My DecimalFormat.format() method for some reason changes things like “2.5” to “2,5”!

That’s why, if result is, for example, 2.5 (as opposed to 3,33333… and such), my statement


is false, not true, as I intended

Here’s how I imported the class

import java.text.DecimalFormat;

Here’s how I created a DecimalFormat object

private static final DecimalFormat df = new DecimalFormat("#.##");

Why would the method do such a weird thing and how do I fix it?

In case you need the entire code, it’s here (it’s too big to paste here, I think)

Localization, most likely. Not all regions of the world write numbers the same way.

DecimalFormat.getDecimalFormatSymbols().getDecimalSeparator() will return what Java considers to be the correct decimal separator, if you want to verify it.

1 Like

Here’s how I fixed it. I still don’t get what caused the problem in the first place. Dots are the default, aren’t they?

static Locale currentLocale = Locale.getDefault();
static DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(currentLocale);
private static final DecimalFormat df = new DecimalFormat("#.##", otherSymbols);


public static void main(String[] args) {


Not necessarily, as you’ve discovered. :slight_smile: