This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes double to string conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "double to string conversion" Watch "double to string conversion" New topic
Author

double to string conversion

rutuja patil
Greenhorn

Joined: Dec 17, 2009
Posts: 23
on jsp page there is price var with 0.0003 value

and on javascript I am accessing that variable using the following code:

output: alert(3E-4)
javascript converts pr.value to 3E-4.
but I want the string value 0.0003 .
Is there any way to convert double to string in javascript?

Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60082
    
  65

What kind of element is "Price"? If it's a form control, the value is already a string and the conversion likely happened during the HTML generation.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
rutuja patil
Greenhorn

Joined: Dec 17, 2009
Posts: 23
price is of double type.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60082
    
  65

There's no such thing in JavaScript. If you are not going to answer the questions posed to you, it's going to be hard to help you.

Look at the View Source in the browser. Is the value of that element already converted to the wrong format?

How are you setting the value of the element in the JSP?

And, by the way, a double is a really poor choice to represent a currency value.
rutuja patil
Greenhorn

Joined: Dec 17, 2009
Posts: 23
I am sorry Bear Bibeault,I didn't understand the question before.now I got it what you tried to ask.
following is the original code of JSP:

output: $0.0003

In above code price is field(double type) of promo object. I use Spring framework for my application.

after reading your message I modified the code to display the original value of object

output: 3.0E-4

After modification it was showing 3.0E-4.
Please let me know if you need any more details.
Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60082
    
  65

Showing the JSP source is useless. The HTML being sent to the browser is what is of interest.
rutuja patil
Greenhorn

Joined: Dec 17, 2009
Posts: 23
I looked at the View Source in the browser. The value of that element is already converted to the wrong format and it is 3.0E-4.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60082
    
  65

Then the problem is on the server. You'll need to make sure that the value is converted in the manner you desire in your JSP.
rutuja patil
Greenhorn

Joined: Dec 17, 2009
Posts: 23
Thanks for your reply. one more question- which type will be better choice for currency?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60082
    
  65

Depends upon a number of factors, but you need one that doesn't lose precision the way that double does.
 
Consider Paul's rocket mass heater.
 
subject: double to string conversion
 
Similar Threads
html output problem
javascript in netscape4.7
passing javascript array from jsp to servlet
onDblClick with Listbox values
how to display decimal point