aspose file tools*
The moose likes Beginning Java and the fly likes HELP! Need to disable scientific notation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "HELP! Need to disable scientific notation" Watch "HELP! Need to disable scientific notation" New topic
Author

HELP! Need to disable scientific notation

Greg Kelly
Greenhorn

Joined: Jun 17, 2001
Posts: 2
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
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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 .


"JavaRanch, where the deer and the Certified play" - David O'Meara
Conrad Kirby
Ranch Hand

Joined: Jun 17, 2001
Posts: 178
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

Joined: Jun 17, 2001
Posts: 2
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

Joined: Jun 17, 2001
Posts: 178
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

Joined: Jun 17, 2001
Posts: 178
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
Pierrick Laforet
Greenhorn

Joined: Jun 18, 2001
Posts: 1
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

Joined: Jun 17, 2001
Posts: 178
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
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5018
    
    8

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.

Junilu - [How to Ask Questions] [How to Answer Questions]
Conrad Kirby
Ranch Hand

Joined: Jun 17, 2001
Posts: 178
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.
+ + = !
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HELP! Need to disable scientific notation