سؤال

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?

هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top