Results 1 to 5 of 5
  1. #1
    innovo is offline Member
    Join Date
    Jul 2015
    Posts
    84
    Rep Power
    0

    Default convert to double??

    Hello once more, I was just doing things in java and I wanted to get something from a text field and have whatever is typed into that come out as a double datatype

    for example, to convert to string you simply use "toString()"

    What I am trying to do is let a user type in a number and that number becomes a double
    Java Code:
    private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {                                            
    double x= jTextField2.getText().toDouble();    // System.out.println(jTextField2.gettext().toString()); // public CsvFileWriter.getText(jComboBox2);  // TODO add your handling code here:
        }
    But there is a problem with this, as far as I'm aware there isn't a "toInt" or "toDouble" that I can use.

    Is there a better way of doing this or is there a way to convert to double?

  2. #2
    Xen
    Xen is offline Member
    Join Date
    Jan 2015
    Posts
    86
    Rep Power
    0

    Default Re: convert to double??

    You can use Double.parseDouble( string value );. This is a static function, that means it operates on the value (the parameter) without being instantiated (relating to an existing object). And it yields a primitive double.
    Last edited by Xen; 08-05-2015 at 03:35 AM.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: convert to double??

    Double.parseDouble(String) returns a double, not a Double.
    Double (Java Platform SE 8 )

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: convert to double??

    Quote Originally Posted by Xen View Post
    To then get a "double" (primitive type) you can do:

    Java Code:
    Double.parseDouble(string).doubleValue()
    .
    Uh, not quite. Double.parseDouble(String) returns a primitive double. So your suggestion will result in a compilation error.

    Regards,
    Jim
    Last edited by jim829; 08-04-2015 at 10:05 PM. Reason: Nuts! Outdrawn by Darryl
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Xen
    Xen is offline Member
    Join Date
    Jan 2015
    Posts
    86
    Rep Power
    0

    Default Re: convert to double??

    Right. Didn't know that. Not that I use doubles all the time ;-). As in, never :p.

Similar Threads

  1. Convert String to double?
    By Monteezy in forum New To Java
    Replies: 9
    Last Post: 11-25-2013, 08:27 PM
  2. convert double to int in ArryList
    By vector_ever in forum New To Java
    Replies: 15
    Last Post: 08-05-2013, 09:08 PM
  3. Convert from string to double
    By Lord ice in forum New To Java
    Replies: 4
    Last Post: 12-12-2010, 05:27 PM
  4. How to convert a double into a int?
    By tyang in forum New To Java
    Replies: 4
    Last Post: 02-10-2010, 10:02 AM
  5. convert String to Double
    By azurovyhrosik in forum CLDC and MIDP
    Replies: 5
    Last Post: 10-22-2008, 02:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •