Win a copy of High Performance Python for Data Analytics this week in the Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

HELP! Need to disable scientific notation

 
Greenhorn
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I'm writing some code that involves converting a double to a string. The problem is that when I use the toString() function to convert it, it automatically changes it to scientific notation (ie. 0.001005 = 1.005E-3). The parser I have to deal with can't work with numbers in this format so I need to figure out how to prevent Java from converting my number to sci notation.
Any help would be greatly appreciated because I have to have this done for tomorrow!
Thanks,
Greg
 
"The Hood"
Posts: 8521
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you convert your Double to a BigDecimal and then use the toString() method of BigDecimal then you can control the rounding behavior of the result.

I have not actually tried this, so tell us how it works .
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually when you convert the double to a String, the double is already stored in scientific notation. If you had a number like .123, when you converted it to a string it would still be .123. Recently I've been working on a formatted rounding function, so I learned a lot of new things about how Java deals with doubles and Strings. As far as helping you, I think what Cindy Glass said was some pretty good advice.
I'd like to hear how it works also.
[This message has been edited by Conrad Kirby (edited June 17, 2001).]
 
Greg Kelly
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your quick replies..
OK, so I tried out the BigDecimal thing, I created a double with a value of "0.00012345" and converted it to a BigDecimal and got 0.00012344999999999999203137424075293893110938370227813720703125
If it hadn't added those extra load of digits at the end it would have worked perfectly because converting that to a string yields the same result.
Does anybody know how to get it to maintain its actual value?
Thanks,
Greg
 
Conrad Kirby
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I really can't help you right now. As a last resort (although it would probably be a good idea to do this anyway) I would buy a good Java reference book. I'm thinking there may be a Double class, but I'm not positive. I'll check tommorow and see what I can find. In the mean time, see if you can parse the double (put it into a String, then chop off the 'exxxx'). Or try multiplying it by 1000 or something like that. I have a feeling you won't be able to get the precise decimal value anyway.
I wish you luck!
 
Conrad Kirby
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wait a minute, I just realized what you were saying. Instead of using the toString() method, just create an instance of a String and have the double value as a parameter. Look:
double myDouble = .00123 ;
String myString = new String (myDouble) ;
Now whatever value the myDouble value is, it will be recognized as a string in the myString object. It will only be in scientific notation when myDouble is too small too handle anyway. You can't really get around Java's way of storing doubles.
Hope that helps
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think that String does not have a constructor with double as parameter. Try this :
double d = 0.001005;
String s = new String();
s = s.valueOf(d);
 
Conrad Kirby
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually I looked it up and your right. However that code still works for some reason. To be sure it works you could do this instead (kindof like typecasting to a String):

That'll do it
 
Sheriff
Posts: 16049
266
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Conrad,
the "+" operator is overloaded for use as a String concatenation operation. See the JLS - string concatenation + operator (�15.18.1). When one operand is a String, other operands are converted to String and the result will be a new String object.
 
Conrad Kirby
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exactly, when you really look deep, it pretty much does turn the double into type char[]. Or am i wrong. I know its the cancatination operator by the way, I've used it many a times.
+ + = !
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in android studio you can see the result.
put a breakpoint on the double for instance 5.208333277550992E-6
new BigDecimal(5.208333277550992E-6).toString()
will print the whole thing
0.0000052083332775509916245937347412109375
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic