unleashed
unleashed

Reputation: 923

How to prevent scientific display of a float?

I have the following output

output =4.08E-4

output =8.9E-5

output =0.978461

output =0.224577

Now the thing I don't get is for 4.08E-4 - I assume it is a negative exponential and given <0 it returns true but is there another way of displaying this in decimal format?

Upvotes: 1

Views: 3115

Answers (4)

Rushdi Shams
Rushdi Shams

Reputation: 2423

I use NumberFormat class-

        double d = 0.000408;            
        //For considering 4 digits after decimal place
        NumberFormat nf = NumberFormat.getInstance();
        nf.setMaximumFractionDigits(4);            
        nf.setGroupingUsed(false);

        System.out.println(d);
        System.out.println(nf.format(d));

Upvotes: 1

Francisco Paulo
Francisco Paulo

Reputation: 6322

Most likely you are trying to print a float/double using System.out.println(...).

This eventually calls the public static String toString() method of Float (or Double). Either way, if you read the Javadoc it states:

If m is less than 10-3 or greater than or equal to 107, then it is represented in so-called "computerized scientific notation." Let n be the unique integer such that 10n <= m < 10n+1; then let a be the mathematically exact quotient of m and 10n so that 1 <= a < 10. The magnitude is then represented as the integer part of a, as a single decimal digit, followed by '.' ('\u002E'), followed by decimal digits representing the fractional part of a, followed by the letter 'E' ('\u0045'), followed by a representation of n as a decimal integer, as produced by the method Integer.toString(int).

You can get around this using System.out.printf(), like this:

double d = 0.000408;

System.out.println(d);
System.out.printf("%f", d);

This prints:

4.08E-4
0,000408

My 2 cents.

Upvotes: 4

Mike Schachter
Mike Schachter

Reputation: 156

I think what you're looking for is either the class NumberFormat or the printf function, either one would work.

Upvotes: 0

icyrock.com
icyrock.com

Reputation: 28618

System.out.println(4.08e-4);

0.000408

Upvotes: 1

Related Questions