wood burning stoves 2.0*
The moose likes Java in General and the fly likes exponential to float Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "exponential to float" Watch "exponential to float" New topic
Author

exponential to float

krishna prasad gunasekaran
Ranch Hand

Joined: Jul 25, 2006
Posts: 158
good day,
how to convert from exponential(E) to ordinary float.

example : 1234.56E2 to 123456.00

have a nice day
krishna prasad


have a great day,
krishna prasad
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30138
    
150

Krisp,
What data type is 1234.56E2? Java doesn't have a built in exponential type per se. It does store/display float/double numbers in that form if they are outside a certain threshold.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
If 1234.56E2 is a String, you can convert to a float or double primitive with Float.parseFloat() or Double.parseDouble().

[Jeanne]: It does store/display float/double numbers in that form if they are outside a certain threshold.

The way the values are stored is exactly the same regardless of how big it is. Only the display format is different depending on the size.

Krisp, if you have

then x and y are in fact exactly the same, and both will be displayed the same way if you print them.


"I'm not back." - Bill Harding, Twister
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

Originally posted by Jim Yingst:

then x and y are in fact exactly the same, and both will be displayed the same way if you print them.


Danger Will Robinson!
Floating point comparisons are not obvious. While Jim's example happens to be the same, just because the printed values of a float are the same, don't expect the values to be the same.

You are much safer using a fuzzy comparison:

if ((x-y) < 0.000001) then System.out.println("they are equal");
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Pat]: While Jim's example happens to be the same,

Mmmm, they don't just happen to be the same - they're guaranteed to be the same. As long as both x and y are in the same JVM, anyway. Your more general point about avoiding using == for floating point is valid in most cases. But my point was that internally, both numbers are represented the same way regardless of whether exponential notation was used in the floating-point literal or not
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Oh, and the fuzzy comparison would be better as

or even



[ December 11, 2007: Message edited by: Jim Yingst ]
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30138
    
150

Originally posted by Jim Yingst:
[Jeanne]: It does store/display float/double numbers in that form if they are outside a certain threshold.

The way the values are stored is exactly the same regardless of how big it is. Only the display format is different depending on the size.

Yes. And I clearly should have made that two sentences so it would have been clearer that my if statement only referred to the display part. Java's easier. Doesn't let one get away with being imprecise
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

But most definitely not if it looks like



[/CODE]
[ December 12, 2007: Message edited by: Pat Farrell ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Most definitely not what? I'm not sure which part of Jeanne's statement you're responding to.

In general I'd write some utility methods:

Then most use cases can be handles cleanly and clearly with something like

Usually I'd prefer relative error. In some cases absolute error may be preferable, especially if both values are close to zero. But even then, relative error may be preferable. Depends on the situation.

Anyway, this has nothing to do with the original question. I don't know if Krisp still has questions about that or not.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

my 'altered' code snippet is guaranteed to generate a divide by zero exception. Not what most folks expect in an equals() comparison.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3432
    
  12
Originally posted by Pat Farrell:
my 'altered' code snippet is guaranteed to generate a divide by zero exception. Not what most folks expect in an equals() comparison.


No it's not, but it does produce an unexpected (to me anyway) result


The result I get is
x == y is true
they are not equal


Joanne
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: exponential to float
 
Similar Threads
primitive float help
3 Questions
Float Display ...
Float Display
Range of integer types