Best way to parseDouble with comma as decimal separator?
Use java.text.NumberFormat:
NumberFormat format = NumberFormat.getInstance(Locale.FRANCE);
Number number = format.parse("1,234");
double d = number.doubleValue();
Updated:
To support multi-language apps use:
NumberFormat format = NumberFormat.getInstance(Locale.getDefault());
How to parse String, with both decimal separators comma and dot as well, to Double
Like Mohit Thakur said, but compilable.
NumberFormat format = NumberFormat.getInstance(Locale.FRANCE);
try {
Number number = format.parse(formDto.getWeight().replace('.', ','));
kitten.setWeight(number.doubleValue());
} catch (ParseException e) {
e.printStackTrace();
}
Parse strings to double with comma and point
You want to treat dot (.
) like comma (,
). So, replace
if (double.TryParse(values[i, j], out tmp))
with
if (double.TryParse(values[i, j].Replace('.', ','), out tmp))
Is there a standard way to parse a float using a custom decimal separator
Alternative 1: Replace "," with "." (as already suggested)
Alternative 2: Use DecimalFormat
float value;
DecimalFormat df = new DecimalFormat();
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator(',');
df.setDecimalFormatSymbols(symbols);
try {
Number n = df.parse("1,234");
value = n.floatValue();
} catch (ParseException e) {
e.printStackTrace();
}
Alternative 3: Use locale
Convert a String to Double - Java
Have a look at java.text.NumberFormat
. For example:
import java.text.*;
import java.util.*;
public class Test
{
// Just for the sake of a simple test program!
public static void main(String[] args) throws Exception
{
NumberFormat format = NumberFormat.getInstance(Locale.US);
Number number = format.parse("835,111.2");
System.out.println(number); // or use number.doubleValue()
}
}
Depending on what kind of quantity you're using though, you might want to parse to a BigDecimal
instead. The easiest way of doing that is probably:
BigDecimal value = new BigDecimal(str.replace(",", ""));
or use a DecimalFormat
with setParseBigDecimal(true)
:
DecimalFormat format = (DecimalFormat) NumberFormat.getInstance(Locale.US);
format.setParseBigDecimal(true);
BigDecimal number = (BigDecimal) format.parse("835,111.2");
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.
Related Topics
Draw a Line in a Jpanel With Button Click in Java
How to Pretty Print Xml from Java
Overriding the Java Equals() Method - Not Working
Should I Always Use a Parallel Stream When Possible
Scanning Java Annotations At Runtime
Classpath Including Jar Within a Jar
Appending to an Objectoutputstream
Jcomponents Not Showing Up With Picture Background
Try/Catch With Inputmismatchexception Creates Infinite Loop
Why Does the Default Object.Tostring() Include the Hashcode
What Are Enums and Why Are They Useful
How to Take a Screenshot Using Java and Save It to Some Sort of Image