It's not a secret anymore!
The moose likes Java Micro Edition and the fly likes Floating point to String conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "Floating point to String conversion" Watch "Floating point to String conversion" New topic

Floating point to String conversion

Roel Appel

Joined: Jun 23, 2008
Posts: 1
Hello all,

I'm developing for BlackBerry 4.2.1 and i'm facing a difficult problem. I want to convert a float to String without the scientific notation. I'm using Float#toString(float f) but it gives an scientific notation back. According to the javadoc, "If m is less than 10-3 or not less than 107, then it is represented in so-called "computerized scientific notation."".

Another problem is there's no NumberFormat class available either.

So i started writing my own formatter, the requirement is formatting floating point numbers with a maximum precision of 10. This is what i have so far:

This works but not for big floating point numbers because of line 24. Here i try to calculate the remainder.
I do this by subtracting the float value with the int value. But first multiply it with a large number because subtracting float numbers is imprecise. The problem is the multiplication with the large number (pow) which can exceed the maximum long size.

Anyway, some help with setting the remainder in the StringBuffer would be appreciated.

Thanks in advance.
I agree. Here's the link:
subject: Floating point to String conversion
jQuery in Action, 3rd edition