Jump to content

JAVA: double to String

Featured Replies

I´m currently trying out Java. So far, my impressions are mixed but tending towards "it´s nice". However, there is one issue that bugs me a bit:

 

I have not yet found out how to convert a double to a string with a reasonable formatting. The toString()-metod and the string = ""+double trick format 1.3322676295501878E-15 to 1.332267 which really is more than useless (in fact, I encoutered the problem because I was looking for a bug - I knew the value had to be O(10^-10) or smaller).

I have already taken a small look at google and to my surprise, I found a lot of classes converting doubles to strings. But I didn´t find what I was looking for: A native Java method (by that I mean a method/function of the core language) doing so.

 

Is there really no native Java method converting a double to a string or did I simply not find it, yet? I can hardly believe that the developers of Java really think ommiting the exponent on a real is a smart idea.

Does it have number.toPrecision() like javascript?

 

Cheers,

 

Ryan Jones

  • Author
Does it have number.toPrecision() like javascript?

Not sure. At least I didn´t find anything with that name. What would the actual command look like?

 

What I am mainly interested in is the "e-15" part of the number; the number of decimal digits is 2nd priority (two or three would probably be ideal).

Not sure. At least I didn´t find anything with that name. What would the actual command look like?

 

What I am mainly interested in is the "e-15" part of the number; the number of decimal digits is 2nd priority (two or three would probably be ideal).

 

 

Just like that...

 

#Presuado code

 

SomeNumber = 1.2223344*10^20

Someumber.toPrecision(8) (the number to the 8th digit is returned.

 

http://www.google.com/search?hl=en&lr=&safe=off&q=%22.toPrecision%22&btnG=Search

 

...Has examples of the JS syntax.

 

If that does not help then I have no idea...

 

Cheers,

 

Ryan Jones

  • Author

Doesn´t seem to be a known method but thanks for the suggestion.

  • Author

NumberFormat works but it doesn´t help me. As far as I´ve seen, NumberFormat rounds the number to the specified number of fractional digits. I really don´t want to carry along 15 digits just to diplay 10^-15.

  • Author

^^ That sounds exactly like what I looked for (even though for the original problem it comes a bit late since I designed the program such that the numbers are always O(1)). Thank you!

Archived

This topic is now archived and is closed to further replies.

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.