Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Double.valueof wont work for formated string

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

    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!


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Double.valueof wont work for formated string

    Stackoverflow: Convert a String to Number

    copied code fragment:

    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()
        }
    }

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    sstavrou (February 8th, 2013)

  4. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Double.valueof wont work for formated string

    oh this seems to work as I wanted! thx!

Similar Threads

  1. Print 000 infront of String, I can get INT to work but not string
    By keat84 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 1st, 2012, 11:23 PM
  2. [SOLVED] Read double from console without having to read a string and converting it to double.
    By Lord Voldemort in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 26th, 2011, 08:08 AM
  3. File IO Compiles, but wont work?
    By StarKannon in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: February 2nd, 2011, 09:05 AM
  4. [SOLVED] Simple While loop wont work??
    By chuckie987 in forum Loops & Control Statements
    Replies: 1
    Last Post: January 31st, 2011, 02:58 PM
  5. [SOLVED] Simple Grahpic porgram wont work.
    By kogo50 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 8th, 2010, 12:46 PM