timo Posted March 6, 2006 Share Posted March 6, 2006 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. Link to comment Share on other sites More sharing options...
RyanJ Posted March 6, 2006 Share Posted March 6, 2006 Does it have number.toPrecision() like javascript? Cheers, Ryan Jones Link to comment Share on other sites More sharing options...
timo Posted March 6, 2006 Author Share Posted March 6, 2006 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). Link to comment Share on other sites More sharing options...
RyanJ Posted March 6, 2006 Share Posted March 6, 2006 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 Link to comment Share on other sites More sharing options...
timo Posted March 6, 2006 Author Share Posted March 6, 2006 Doesn´t seem to be a known method but thanks for the suggestion. Link to comment Share on other sites More sharing options...
bascule Posted March 6, 2006 Share Posted March 6, 2006 Seems like you want NumberFormat: http://java.sun.com/j2se/1.4.2/docs/api/java/text/NumberFormat.html Specifically use the .format() method to convert from a double to a string p.s. Java sucks Link to comment Share on other sites More sharing options...
timo Posted March 6, 2006 Author Share Posted March 6, 2006 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. Link to comment Share on other sites More sharing options...
Dave Posted March 12, 2006 Share Posted March 12, 2006 How about DecimalFormat? It's a subclass of NumberFormat designed for decimal numbers. Example: "0.###E0" formats the number 1234 as "1.234E3". Link to comment Share on other sites More sharing options...
timo Posted March 12, 2006 Author Share Posted March 12, 2006 ^^ 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! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now