String to Double conversion

Posted on 2011-05-04
Last Modified: 2012-05-11
Hi There,

I am trying to set "002147483647" into a setter of type Double.

i.e amount.setAmount(new Double(002147483647/100);

private Double amount;
public void seAmount(Double amount) {
      this.Amount = amount;

but when I tried to get the value, I am getting as 2.147483647E7

Please advice.

Question by:theartha
    LVL 23

    Expert Comment

    I believe it just prints it in scientific form.

    Value is correct only.
    LVL 86

    Expert Comment

    That's just a string representation. Format it (as a String) however you want it

    Author Comment

    @cmalakar: Yes the value is correct, but I can't use that value to insert or get a value from database.

    i.e: select year from employee where employeeAmt = "2.147483647E7" -- fails
     select year from employee where employeeAmt = "21474836.47" -- pass

    LVL 23

    Accepted Solution

    You can convert to required format using DecimalFormat class.

    Check examples here..
    LVL 86

    Expert Comment

    >>i.e: select year from employee where employeeAmt = "2.147483647E7" -- fails

    ... but you wouldn't do it that way. You'd use a PreparedStatement
    LVL 86

    Assisted Solution

    double d = 2.147483647E7;
    PreparedStatement ps = conn.prepareStatement("select year from employee where employeeAmt = ?");
    ps.setDouble(1, 2.147483647E7);
    ps.setDouble(1, d);// << OR THIS

    Open in new window

    LVL 92

    Assisted Solution

    LVL 2

    Assisted Solution

    Double will support only 7 integer digits and 8 decimal places. That is a double number can be represented as a.b where a can have a maximum value of 9999999 and b
    can have a maximum value of 99999999.

    Now if the value of a is greater than the above specified value , then the total value will be represented in exponential form (E). That is like 2.147483647E7 which means 2.147483647 x 10^7  (Ten raised to Seven. That is 10 x 10 x 10....7 times.) , which is equal to 21474836.47

    Hope you understood this little explanation...
    LVL 2

    Expert Comment

    In your operation

    amount.setAmount(new Double(002147483647/100);

    2147483647 / 100 = 21474836.47

    So taking the above explanantion , you can see that the result is having 8 integer digits and 2 decimal digits , which is the reason why the result (output) is converted to exponential form (E).


    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    java constructor error 8 65
    JKS to store upstart data 2 51
    array6 challenfge 6 48
    create a gui in perl 3 22
    This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    25 Experts available now in Live!

    Get 1:1 Help Now