I want to convert some numbers which I got as strings into Doubles, but these numbers are not in US standard locale, but in a different one. How can I do that?
-
2I have six answers, only two of them upvoted, but ALL of them are correct. The laurels go to the unabridged answer. – Daniel C. Sobral May 20 '09 at 14:41
8 Answers
Try java.text.NumberFormat
. From the Javadocs:
To format a number for a different Locale, specify it in the call to getInstance.
NumberFormat nf = NumberFormat.getInstance(Locale.FRENCH);
You can also use a NumberFormat to parse numbers:
myNumber = nf.parse(myString);
parse()
returns a Number
; so to get a double
, you must call myNumber.doubleValue()
:
double myNumber = nf.parse(myString).doubleValue();
Note that parse()
will never return null
, so this cannot cause a NullPointerException
. Instead, parse
throws a checked ParseException
if it fails.
Edit: I originally said that there was another way to convert to double
: cast the result to Double
and use unboxing. I thought that since a general-purpose instance of NumberFormat
was being used (per the Javadocs for getInstance
), it would always return a Double
. But DJClayworth points out that the Javadocs for parse(String, ParsePosition)
(which is called by parse(String)
) say that a Long
is returned if possible. Therefore, casting the result to Double
is unsafe and should not be tried!
Thanks, DJClayworth!

- 1
- 1

- 188,989
- 46
- 291
- 292
-
Add the .doubleValue() method to the answer and I'll accept it. – Daniel C. Sobral May 20 '09 at 14:17
-
I see nothing to promise that NumberFormat.parse() returns a Double. In fact it appears to say that if the value can be contained in a Long, it returns Long, causing your method 1 to throw a ClassCastExeption. – DJClayworth May 20 '09 at 15:42
-
10Be careful with French, because the FR_fr locale uses a non-breaking space (U+00A0) as the grouping separator. This causes the text to appear the same as if the separator were a "regular" space (U+0020) (e.g., standard keyboard space). The result is a silent failure where a number string that appears to be correctly formatted like "3 141,59" is parsed to 3 instead of 3141.59 . The string must be specified like "3\u00A0141,59" for it to parse correctly. Also, it's good practice to use the method with ParsePosition and check ParsePosition.getIndex() == inputString.length() after parsing. – Phil Oct 30 '09 at 07:10
-
You might want to use "Locale.getDefault()", I wasted a couple of minutes before noticing why my "222,333" string was parsed as a "222" int. – Buffalo Mar 10 '14 at 14:40
-
This also fails when the String is in scientific notation. For instance "1.93263561400442e-006" results into 1.93263561400442 – akrog Jun 04 '14 at 16:06
NumberFormat is the way to go, but you should be aware of its peculiarities which crop up when your data is less than 100% correct.
I found the following usefull:
http://www.ibm.com/developerworks/java/library/j-numberformat/index.html
If your input can be trusted then you don't have to worry about it.

- 4,632
- 6
- 26
- 21
-
1Could you summarize what was interesting in the link if you remember? It's broken now... – Matthieu Jun 28 '21 at 10:02
Just learning java and programming. Had similar question. Found something like this in my textbook:
Scanner sc = new Scanner(string);
double number = sc.nextDouble();
The book says that a scanner automatically decodes what's in a String variabel and that the Scanner class automatically adapts to the language of the set Locale, system Locale being the default, but that's easy to set to something else.
I solved my problem this way. Maybe this could work for the above issue instead of parsing?
Addition: The reason I liked this method was the fact that when using swing dialouge boxes for input and then trying to convert the string to double with parse I got a NumberFormatException. It turned out that parse exclusively uses US-number formatting while Scanner can handle all formats. Scanner made the input work flawlessly even with the comma (,) decimal separator. Since the most voted up answer uses parse I really don't see how it would solve this particular problem. You would have to input your numbers in US format and then convert them to your locale format. That's rather inconvenient when ones numeric keybord is fitted with a comma.
Now you're all free to shred me to pieces ;)

- 61
- 1
- 3
-
2The comments (in Android Studio at least) for java.util.Scanner are kind of funny... `A parser that parses a text string of primitive types and strings with the help of regular expressions. This class is not as useful as it might seem. It's very inefficient for communicating between machines; you should use JSON, protobufs, or even XML for that. Very simple uses might get away with String#split. For input from humans, the use of locale-specific regular expressions make it not only expensive but also somewhat unpredictable.` – Andrew Kirkegaard Feb 03 '15 at 19:55
You use a NumberFormat
. Here is one example, which I think looks correct.

- 391,730
- 64
- 469
- 606
Do you know which locale it is? Then you can use
DecimalFormat format = DecimalFormat.getInstance(theLocale);
format.parse(yourString);
this will even work for scientific notations, strings with percentage signs or strings with currency symbols.

- 19,463
- 14
- 75
- 113
-
1DecimalFormat.getInstance is not guaranteed to return a DecimalFormat, actually. You're really calling NumberFormat.getInstance, which is only guaranteed to return a subclass of NumberFormat. From NumberFormat's docs: "If you want even more control over the format or parsing, or want to give your users more control, you can try casting the NumberFormat you get from the factory methods to a DecimalFormat. This will work for the vast majority of locales; just remember to put it in a try block in case you encounter an unusual one." – Michael Myers May 20 '09 at 14:16
-
You are right indeed. In my code, I actually use DecimalFormat format = (DecimalFormat)DecimalFormat.getInstance(theLocale); I didn't know it doesn't work for all locales... – Fortega May 20 '09 at 14:53
Here is how you use parseDouble
to convert a String
to a Double
:
doubleExample.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class doubleExample {
public static void main(String[] args) {
Double myDouble = new Double("0");
System.out.println("Please enter a number:");
try
{
//get the number from console
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
myDouble = Double.parseDouble(br.readLine());
}
//if invalid value was entered
catch(NumberFormatException ne)
{
System.out.println("Invalid value" + ne);
System.exit(0);
}
catch(IOException ioe)
{
System.out.println("IO Error :" + ioe);
System.exit(0);
}
System.out.println("Double value is " + myDouble);
}
}

- 3,673
- 4
- 29
- 46

- 21
- 1
-
What does your answer adds to previous accepted and high upvoted answers? Care to comment your code a bit? – Yaroslav Oct 09 '12 at 13:59
-
3This answer is *DOWNRIGHT INCORRECT*. According to Java API documentation, `parseDouble` uses the same rules as `valueOf`, which are Java *Language* double format. That means it won't parse `1,0` as double, no matter the current locale. And, furthermore, there's no way to pass a *specific* locale, as asked for (which makes sense, since it doesn't use locale at all). – Daniel C. Sobral Oct 11 '12 at 17:12