Double.parseDouble

the user enters a premium amount and then I want to use that amount in a calcuation; can't I parseDouble it like this? apparently not, because when I try to multiply, I get an error "the operator * is undefined for argument type(s) double, String. I have done this in the past, what is wrong?
String pAmount = JOptionPane.showInputDialog("Enter the premium amount:");
double num = EndCancelDiffCounter;
               double denom = counter;
               double result = num/denom;
               Double.parseDouble(pAmount);
               double refund = result * pAmount;

parseDouble returns a double, it does not change the
String given froma String to a double. You need to
store that result (return value) in a new double
variable and perform your calculation with that.
Edit: Man I'm slow. (I had to check that
parseDouble returned a double and not a Double. I
wasn't 100% sure, as I'm a little daft this morning.)I see, thank you

Similar Messages

  • Double.parseDouble(String) - problems when string is in scientific notation

    Hello guys,
    I'm doing some numerical calculations and I wonder whether it is possible for Double.parseDouble(String) to parse string in the scientific notation i.e. 1.0824234234E-10. Is it the notation itself causing the exception : NumberFormatException or the number is just too big/small and double can't hold it ?
    If it's just the notation how can I fix it ?
    Regards

    i'm not quite sure whether double odoes not allow it.
    perhaps consider the api Double.valueOf() and the testing code provided; reproduced below:To avoid calling this method on a invalid string and having a NumberFormatException be thrown, the regular expression below can be used to screen the input string:
            final String Digits     = "(\\p{Digit}+)";
      final String HexDigits  = "(\\p{XDigit}+)";
            // an exponent is 'e' or 'E' followed by an optionally
            // signed decimal integer.
            final String Exp        = "[eE][+-]?"+Digits;
            final String fpRegex    =
                ("[\\x00-\\x20]*"+  // Optional leading "whitespace"
                 "[+-]?(" + // Optional sign character
                 "NaN|" +           // "NaN" string
                 "Infinity|" +      // "Infinity" string
                 // A decimal floating-point string representing a finite positive
                 // number without a leading sign has at most five basic pieces:
                 // Digits . Digits ExponentPart FloatTypeSuffix
                 // Since this method allows integer-only strings as input
                 // in addition to strings of floating-point literals, the
                 // two sub-patterns below are simplifications of the grammar
                 // productions from the Java Language Specification, 2nd
                 // edition, section 3.10.2.
                 // Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt
                 "((("+Digits+"(\\.)?("+Digits+"?)("+Exp+")?)|"+
                 // . Digits ExponentPart_opt FloatTypeSuffix_opt
                 "(\\.("+Digits+")("+Exp+")?)|"+
           // Hexadecimal strings
           "((" +
            // 0[xX] HexDigits ._opt BinaryExponent FloatTypeSuffix_opt
            "(0[xX]" + HexDigits + "(\\.)?)|" +
            // 0[xX] HexDigits_opt . HexDigits BinaryExponent FloatTypeSuffix_opt
            "(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" +
            ")[pP][+-]?" + Digits + "))" +
                 "[fFdD]?))" +
                 "[\\x00-\\x20]*");// Optional trailing "whitespace"
      if (Pattern.matches(fpRegex, myString))
                Double.valueOf(myString); // Will not throw NumberFormatException
            else {
                // Perform suitable alternative action
    http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Double.html

  • Double.parseDouble(String) : Problem to parse large String

    Hello,
    I need to convert A String to a Double in my application without rounding of digits.
    I have used the Double.parseDouble(String) method.
    The maximum string length can be 17 including dot(.)
    So if my String is (of length 17)
    Eg.
    S= �9999999999.999999� then I am getting the double value as
    d= 9999999999.999998(compare the last digit)
    If s = �9999999999.111111� then d = 9999999999.111110
    If s = �9999999999.555555� then d = 9999999999.555555 (result that I want)
    If s = �9999999999.666666� then d = 9999999999.666666 (result that I want)
    If s = �9999999999.777777� then d = 9999999999.777777 (result that I want)
    If s = �9999999999.888888� then d = 9999999999.888887
    If s = �9999999999.999999� then d = 9999999999.999998
    If s = �9123456789.123456� then d = 9123456789.123456
    But string length up to 16 is giving me the accurate result
    So any body can explain me that why it is happening? And how can i get the accurate result.
    Thanks in advanced.

    Hi,
    Thank You for your suggestion. By which i can store
    the big double number in Data base , but at some
    point i require to parse a double value from a Double
    reference.In that case if the Decimal value length
    => 16 then it the last digit changes or rounds.
    coverting 9999999999.999999 Decimal value to double
    value. It gives me 9999999999.999998 and
    9999999999.9999999 gives me the 10000000000.0000000.
    but the Double value 999999999.999999 is ok
    and 999999999.99999999 rounds.Err, is there a follow up question in there?

  • Double.parseDouble help?

    I have this String Number 20.000. When I do a parseDouble I get 20.0 which is right.
    public class qwerty {
         public static void main(String[] args) {
              String num = "20.000";
              System.out.println(Double.parseDouble(num));
    But is there a way to preserve the zeroes and it prints 20.000 itself after parse Double?

    But is there a way to preserve the zeroes and it prints 20.000 itself after parse Double?Not directly. Doubles (and doubles, and all other Number subclasses and number primitives) store only information about the value of the number, not how it is to be displayed.
    As mentioned, you'll need to use formatting provided either by a DecimalFormat object or a printf() function. To do what you're asking, you'd need to somehow parse the input string to determine the required format, and store that information separately (e.g. in a String or a DecimalFormat object), then use that whenever you want to display the number. Check out the documentation for DecimalFormat and PrintStream.printf().
    Regex would probably be helpful if you decide to go that route.

  • Double.parseDouble() is not Locale specific, how do I get round this?

    I am parsing Strings into doubles using
    String myNumber = "12,34";
    double d = Double.parseDouble( myNumber );The problem is that my number is of French style (it has commas instead of fullstops for the decimal points)
    The default Locale is English, so I get NumberFormat problems when parsing. (I can understand this)
    I even get NumberFormat problems when I do this:
    Locale.setDefault(Locale.FRENCH);
    double d = Double.parseDouble( myNumber );Which I think is really strange. I guess the Locale of Double is set at a certain time and not reset.
    OR maybe Double.parseDouble( <STRING> ); is not Locale specific?
    Where is my understanding wrong?

    Do you know if DecimalFormat reflects the default Locale if the default Locale changes?
    Or will I have to make a new DecimalFormat whenever it changes?As Locale is invariant, a DecimalFormat instance (as well a any object with a reference to the default Locale) cannot reflect the default Locale changes.
    But each time you create a new instance of DecimalFormat without specifying the Locale, it will get up-to-date Locale.
    However, you might prefer setting the locale explicitely (instead of changing the default one.)
    Note that Double valueOf() or parseDouble() methods are not related to default Locale, they use a fixed format.

  • What if Textfield t1 is blank?? num1=Double.parseDouble(t1.getText());

    Hi there
    Im coding a simple calculator with 2 Textfield for entering two numbers. If one of Textfields is blank and an operation button (+,-,*,/) is clciked, I want the 3rd Textfield t3 to read "ERROR".
    From the code below, what is num1 if t1 is blank. Or could you tell me how to go about this if statement.
    if (ae.getSource()==multi){
                   num1=Double.parseDouble(t1.getText());
                   num2=Double.parseDouble(t2.getText());
                   res=num1*num2;
                   t3.setText(""+res);
    cheers
    SERGIO

    I look forward to you figuring this out yourself.
    1. What does getText() return when then JTextField is empty?
    2. What happens when you pass that to parseDouble?
    3. Should you even bother passing that to parseDouble?

  • Double.parseDouble lost of precision!

    Hello,
    I have a small problem regarding convertion from string to double.
    double dTotal;
    dTotal = Double.parseDouble("10.01") + Double.parseDouble("0.12");
    Can someone explain me why my result is dTotal = 10.12999999999999 ?
    Thanks in advance.
    Remi.

    and read
    http://docs.sun.com/source/806-3568/ncg_goldberg.html
    This, too:http://java.sun.com/developer/JDCTechTips/2003/tt0204.html#2

  • Problem on Double.parseDouble

    Double.parseDouble(InputString); was error on my NetBeans 6.8 how to fixed it? I've tried to reinstall but still the same

    Make sure you are using CLDC 1.1. Double is not included in CLDC 1.0.
    Erik Wetterberg

  • Does browser support Double.parseDouble()???

    hi,
    i have used Double.parseDouble() method in my program. After i run that program in browser, it shows error "Double.parseDouble() method not found". Doesnt browser support Double.parseDouble()??? Can anybody tell me how to solve this error?? (i have also tried Float.parseFloat(),but same error occurs) Please help me.
    Thanks in advance,
    Kalpana.

    Newer non-Microsoft browsers such as Netscape 6 and Opera 5 support JDK 1.3. I think IE 5.1 for Mac OS X will use the JDK 1.3 installed on the computer, also. It's only older browsers and IE for Windows and Mac OS 9.1 and before that do not support JDK 1.3. Unfortunately, that's 90% of all browsers! :-(

  • Variants of Double.parseDouble

    I have an applet that runs perfectly on appletviewer but screws up when viewed through my web browser. It doesn't parse the string( number ) found in my textfield( input ). I did it like this: "x = Double.parseDouble( input.getText() );". what is the syntax that i should follow to conform with those that my browser could support? Probably an older one. My browser is IE 4 or 5... Thanks!

    Sun must have been struggling for a bit 'cos it wasn't until JDK1.2 that they introduced Double.parseDouble, even though Integer.parseInt has been there since the dawn of time itself.
    And since Microsoft stopped supported Java around JDK1.1.6 you'll probably find that your Applet is complaining that there's no such method (have a look in the Java Console in IE to see what it's got to say for itself).
    There are various things you could do:
    1) install the Java Plug-in to bring your JVM into this century
    2) see if DecimalFormat is supported by your runtime (can't find when it was introduced in the Javadocs - sorry)
    3) write your own simple double-parsing routine
    Personally I'd opt for 1. I'd be tempted to do option 2 as well but it's hardly necessary!
    Hope this helps.

  • Issue with Double.parseDouble

    ok i dono whats going on here im writing a console based bank management program and when converting a input srting from console.readline to Double.parseDouble it says the method parseDouble cannot be found, im using the latest jvm whats going on! help!

    my bad i ment latest jdk not thinking tonight! heres the error:
    D:\My Documents\Java\Bank Acount\AccountDriver.java:59: Method parseDouble(java.lang.String) not found in class java.lang.Double.
                             newAcc = Double.parseDouble (input);
                             ^
    1 error

  • Double.parseDouble returning 2.0E7

    Hi,
    I have a piece of code (snippet below)
    double aggrAmount=0.0;
    Tag t;
    aggrAmount=Double.parseDouble(t.getValue().substring(t.getValue().lastIndexOf("/")+1, t.getValue().length()-1));
    where t="AGGR//FAMT/20000000,"
    when i check aggrAmount the value is 2.0E7. How can i get the value as it is?
    PLs help...
    THanks for your time and advice

    when i check aggrAmount the value is 2.0E7. How can i get the value as it is?There's some confusion here between the value (a double value of twenty million), and the String you use to check it ("2.0E7"). They are different things.
    If you want to see or use the value in a string without the exponent business, use String.format() or DecimalFormat. Note that the value remains what it is (twenty million); numbers don't have a format.
    If you want the last part "as it is" ie as a String use String's lastIndexOf() and substring() methods and make value a String.

  • JDK 1.1.8 and Double.parseDouble

    I've downloaded JDK 1.1.8 from this web site.
    When I try to compile the program which includes converting input data into decimal number the compiler says that it can't find method parseDouble in the class java.lang.Double.
    I know that the source code is right, because I've tried to compile and run this program on another computer. Java applications on that computer are installed from Jbuilder CD.
    What's the problem with JDK 1.1.8?
    Thanx for help

    Depending on which version of JBuilder you have, it will install a JDK that is included with it.
    For example, JBuilder 6 has JDK 1.3.1, which includes the parsing function you are looking for.
    Why would you download 1.1.8 and not the latest version?
    Thanks,
    Haig

  • Double.parseDouble question

    Hello I am creating a program for school. I am using text fields and if you edit one and click modify it will save the changes. The program displays currency and if you change the field and the $ is there I get an error. How can I have the program drop the $.
    Thanks

    extremeshannon wrote:
    Thanks for the response. In the text field it already displays $dollar amount. If the user is modifying the record and doesn't take out the $ it throws an error.You can just not display the $ in the JTextField (this is Swing, right?), or else parse the text using a currency instance of a NumberFormat object.

  • Why aren't format() and parseDouble() or parseInt() compatible??

    Hello, I display numbers to the user in their default locale format with:
    NumberFormat.getInstance().format(number)
    For example, here in the United States, I get a string with commas separating every 3 digits.
    However, when the user naturally wants to use the same format in the input, then
    Double.parseDouble(string), or Integer.parseInt(string)
    throw exceptions, because they don't like the commas.
    This is strange - shouldn't parseDouble and parseInt, accept any locale formatted strings, or, if that would be ambiguous, shouldn't there be a way to tell them what locale we are using?
    I can't just delete the commas myself, because in a different locale, God-knows-what will be there.
    So how do I parse a number string, in a default locale, to a double?? Thank you for any hints.
    Mark

    No, does not work. This only formats from the beginning of the string, leaving the end if there is junk there. So if the user input a "O" for example, instead of zero at the end, they will wonder why the number did not increase. Worse yet, if they input a legitimate string, such as 2.0e6 (which is a good way to input big numbers in a text field), that will not be formatted, but Double.formatDouble() does format it (it just does not like commas).
    To the others answering, thank you for your opinions, but I want the user to be able to input the numbers the same way they are outputted. This is natural way for them to react and it would be unreasonable for me to prevent them.
    As to why I don't want to strip commas myself , I already explained.

Maybe you are looking for

  • List view not showing all podcasts

    Guys, I have the annoying problem that podcast management by itunes and the podcast app are F'ed up. Not too lobg ago I experienced my ipad greying out most podcasts that were downloaded and on my ipad, for no reason at all. it took me 2 days fiddlin

  • Inserting multiple data in a single textfield

    is it possible?? something like "select name from table where department_id = 2" and put it in a textfield(or any prefered item) and the output would be like "sanchez , goodyear , hillman" <-- data shud be also separated by comma.

  • SAP WM Documentation for self study

    Dear All, I am SAP MM consultant & I am interested to learn SAP WM. Can you please help/guide me to get SAP WM documentation for self study. I appreciate your advise to learn SAPWM in better way. Thanks in advance. Regards BSA

  • SAP BW timeout

    I am running a Crystal report against a BW query, via the Business Objects Enterprise XI scheduler, which is returning a successful completion message even if the query times out on the BW server. Any thoughts as to why it should return a "successful

  • Entire cell a hyperlink

    I have a 4x4 table with images and text in each cell.  Is it possible to make the entire cell with both text and image a hyperlink and that it changes color when the user mouses over it.