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

Laazo :

I have the below code for formatting a double to two decimal places;

DecimalFormat df = new DecimalFormat("0.00");
double monthlyVal = forecastReturnValue / months;
return Double.valueOf(df.format(monthlyVal));

The last line fails with a NumberFormatException because decimals are separated by a comma;

Screenshot

I am using windows OpenJDK8:

openjdk version "1.8.0_41"
OpenJDK Runtime Environment (build1.8.0_41-b04)
OpenJDK Client VM (build 25.40-b25, mixed mode)

What could be the cause?

RUARO Thibault :

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);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

comma in csv file is recognized as separator even though it's inside double quote

Replace ,(comma) by .(dot) and .(dot) by ,(comma)

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

Convert String with Dot or Comma as decimal separator to number in JavaScript

no grouping separator char for DecimalFormat

DecimalFormat pattern

Python regex for number with or without decimals using a dot or comma as separator?

How to accept both dot and comma as a decimal separator with WTForms?

Format number in R with both comma thousands separator and specified decimals

How to convert floating point decimal separator from dot to comma in Javascript

Importing xlsx data to R when numbers have a comma as decimal separator

Change the timespan millisecond separator; comma(,) instead of dot(.)

seq uses both comma and dot as a decimal separator

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

Split a string with comma separator but not when enclosed with [ ]

Azure Data Factory: Reading doubles with a comma as decimal separator instead of a dot

How to replace second pattern(dot) after pattern(comma) in bash

Angular uses comma instead of dot as decimal separator after changing culture

dot instead of comma as thousand separator

consider comma as a dot when calculate?

Highlight "red" the text box when value is not numeric, numeric with dot or comma

NumericUpDown: accept both comma and dot as decimal separator

In Java, convert the dot to comma in DecimalFormat replace method

parse double from file with random comma or dot separator

JavaScript regex to accept numbers with comma separator (thousands) and dot separator (decimals)

Decimalformat: unwanted replace of dot with comma in JTable

Comma separator ignored when reading in .csv files to R on a Spanish computer

How to replace decimal separator dot by decimal separator comma in formattable() package R?

c language error when using comma as separator for multiple number inputs