Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java DecimalFormat returns a "?"

My DecimalFormat is sometimes returning a '?' when trying to format(). Is there an input that would create this scenario?

For example:

DecimalFormat df = new DecimalFormat("#.####");
df.format(X); // output : '?'

What could X possibly be?

like image 330
tinkertime Avatar asked Apr 07 '11 16:04

tinkertime


2 Answers

It's not a question mark, it's a U+FFFD REPLACEMENT CHARACTER, which is displayed as ? since it can't be mapped to the output encoding:

NaN is formatted as a string, which typically has a single character \uFFFD. This string is determined by the DecimalFormatSymbols object. This is the only value for which the prefixes and suffixes are not used.

Similarly, ? in representation of infinity is a U+221E INFINITY character (∞).

Infinity is formatted as a string, which typically has a single character \u221E, with the positive or negative prefixes and suffixes applied. The infinity string is determined by the DecimalFormatSymbols object.

See also:

  • DecimalFormat javadoc
like image 176
axtavt Avatar answered Sep 20 '22 20:09

axtavt


It'll return "?" if X is Float.NaN or Float.POSITIVE_INFINITY. It appears that Float.NEGATIVE_INFINITY returns "-?".

like image 32
WhiteFang34 Avatar answered Sep 23 '22 20:09

WhiteFang34