Captain Giraffe
Captain Giraffe

Reputation: 14705

Unexpected scientific formatting on floats

According to the docs

String text = String.format("%5.1e", 3.1415f);

Should produce something like

3.1E+00

On my emulator it is as expected

3.1E+00

On my dev phone it shows as

3.1*10^+00

Which makes it useless for all practical purposes.

The Android documentation does not tell me much about localization but the Oracle docs only mentions localized formatting of the first part "3.1".

Is there a simple way to get consistent formatting?

Is this documented behaviour? a bug?

A HTC Nexus One gives the odd behaviour, a Samsung Galaxy Nexus gives the proper behaviour.

Upvotes: 2

Views: 92

Answers (1)

Jeffrey Blattman
Jeffrey Blattman

Reputation: 22647

It sounds like it's an issue of the N1, or in the version of Android that the N1 is running. One option would be to grab the code from somewhere you know it works, and copy to your own formatter for scientific numbers.

Looking at the source for java.util.Formatter, take a look at this print() method. This seems to be where the magic happens.

Upvotes: 1

Related Questions