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

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

Please advice.

Thanks.
0
theartha
Asked:
theartha
  • 3
  • 2
  • 2
  • +2
4 Solutions
 
cmalakarCommented:
I believe it just prints it in scientific form.

Value is correct only.
0
 
CEHJCommented:
That's just a string representation. Format it (as a String) however you want it
0
 
thearthaAuthor 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Check examples here..

http://www.exampledepot.com/egs/java.text/FormatNum.html
0
 
CEHJCommented:
>>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
 
CEHJCommented:
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();

Open in new window

0
 
objectsCommented:
0
 
Suraj_MathewCommented:
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
 
Suraj_MathewCommented:
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).

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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