throwaway_account
throwaway_account

Reputation: 13

Convert very small double values to string (with scientific notation) (Java)

I'm trying to print a small double number like 6.67e-11, but using Double.toString() returns 0. What can I do to make it print 6.67e-11 (or something similar) instead?

Upvotes: 1

Views: 1627

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500515

Unable to reproduce:

public class Test {

    public static void main(String args[])
    {
        double d = 6.67e-11;

        System.out.println(Double.toString(d)); // Prints "6.67E-11"
    }
}

IIRC, Double.toString() always returns a string which allows the exact value to be round-tripped using Double.parseDouble().

My guess is that you don't actually have a small value - that you have 0, due to some rounding errors in other operations.

Upvotes: 2

Related Questions