• Status: Solved
• Priority: Medium
• Security: Public
• Views: 680

# String to Double conversion

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
theartha
• 3
• 2
• 2
• +2
4 Solutions

Commented:
I believe it just prints it in scientific form.

Value is correct only.
0

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

Author Commented:
@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

Commented:
You can convert to required format using DecimalFormat class.

Check examples here..

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

Commented:
>>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

Commented:
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

Commented:
0

Commented:
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

Commented:

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

• 3
• 2
• 2
• +2
Tackle projects and never again get stuck behind a technical roadblock.