I’m trying to debug an issue involving DecimalFormat#applyPattern(String) in Java 1.6. The default locale is French so the decimal separator by default is ‘,’. However, the pattern specifies applying a ‘.’ to the decimal format instance which it doesn’t seem to. I tried to debug into applyPattern() but couldn’t see the variable values. I’m not really familiar with patterns and reg-ex’s so I’m not sure if the issue is with the pattern or something else. Is it the pattern which is incorrect or there is some other issue with the code?
Here is the code snippet:
public class DecimalFormatTester
{
static private final DecimalFormat doubleFormat = (DecimalFormat) NumberFormat.getInstance();
static
{
doubleFormat.applyPattern("############.00");
}
public static String toString(double value)
{
synchronized (doubleFormat)
{
return doubleFormat.format(value);
}
}
}
Here is a sample line of Junit code which fails:
assertEquals("String values aren't equal", "2179005635.00", DecimalFormatTester.toString(2.179005635E9));
The
.in pattern############.00is a placeholder for the (currently set) decimalCharacter.If you want
.as decimalcharacter then you additionally have to setdecimalCharacter, eg. by setting
Locale.USYou could save your current locale, then set to
Locale.US, then set back your savedLocale,but make sure your app is not multi threaded.
You also could use