Jump to content

Recommended Posts

Posted

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.

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

Posted
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

Posted

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.

Posted

^^ 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!

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

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.