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?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Try
java.text.NumberFormat. From the Javadocs:parse()returns aNumber; so to get adouble, you must callmyNumber.doubleValue():Note that
parse()will never returnnull, so this cannot cause aNullPointerException. Instead,parsethrows a checkedParseExceptionif it fails.Edit: I originally said that there was another way to convert to
double: cast the result toDoubleand use unboxing. I thought that since a general-purpose instance ofNumberFormatwas being used (per the Javadocs forgetInstance), it would always return aDouble. But DJClayworth points out that the Javadocs forparse(String, ParsePosition)(which is called byparse(String)) say that aLongis returned if possible. Therefore, casting the result toDoubleis unsafe and should not be tried!Thanks, DJClayworth!