Solved

# String to Double conversion

Posted on 2011-05-04
639 Views
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

Thanks.
0
Question by:theartha

LVL 23

Expert Comment

I believe it just prints it in scientific form.

Value is correct only.
0

LVL 86

Expert Comment

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

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

0

LVL 23

Accepted Solution

You can convert to required format using DecimalFormat class.

Check examples here..

http://www.exampledepot.com/egs/java.text/FormatNum.html
0

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
0

LVL 86

Assisted Solution

e.g.
``````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
ps.executeQuery();
``````
0

LVL 92

Assisted Solution

0

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...
0

LVL 2

Expert Comment

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).

0

## Featured Post

### Suggested Solutions

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…

#### Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!