简体   繁体   中英

Java: Can't find symbol

In Javadoc it was written that :

public static String toString(double d)

Returns a string representation of the double argument. All characters mentioned below are ASCII characters.

If the argument is NaN, the result is the string "NaN".

But when I am compiling below code it is giving error: Cant find symbol NaN

String intStr2 =Double.toString(NaN); 

由于NaN未定义,因此会抛出编译错误,使用以下方法来克服同样的错误,

String intStr2 = Double.toString(Double.NaN);

Double.NaN is defined in Double.java as (ref jdk8)

/**
 * A constant holding a Not-a-Number (NaN) value of type
 * {@code double}. It is equivalent to the value returned by
 * {@code Double.longBitsToDouble(0x7ff8000000000000L)}.
 */
public static final double NaN = 0.0d / 0.0;

And it is well converted in String "NaN"

String intStr2 =Double.toString(Double.NaN); 
System.out.println(intStr2);

The error NaN is "Not a Number". You'd have to define it first.

String intStr2 = Double.toString(Double.NAN);

You'd be able to throw it in print and it should print. For infinity, you'd have to use(Positive and negative, interchangeable.)

String intStr2 = Double.toString(Double.POSITIVE_INFINITY);
System.out.print(intStr2);

Should print out Infinity

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM