Double.valueof wont work for formated string
Hi all
I got a noob question as I am a begginer in java and new to the forum! I bet its a simple matter but it seems to trouble me :confused:
so I got this String say "123,456.78" which I am trying to store inside a Double but I am getting a numberformatexception. the programm will understand the "123456.78" string but not the one with the thousand seperator!
I guess one option would be to get rid of the "," but I would like this to be clever and depend on my local/regeional setings, so regardelss of the decimal/thousand symbols the program to be able to understand that the above string can be a Double.
any advise/guideline?
thanks!
Re: Double.valueof wont work for formated string
Stackoverflow: Convert a String to Number
copied code fragment:
Code java:
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()
}
}
Re: Double.valueof wont work for formated string
oh this seems to work as I wanted! thx!