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?
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?
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:
It'll return "?" if X
is Float.NaN
or Float.POSITIVE_INFINITY
. It appears that Float.NEGATIVE_INFINITY
returns "-?".
I have just solved very similar problem. In my case I was trying to return currency sign to Spring and display it in Thymeleaf template.
public String getAmountDue() {
DecimalFormat decimalFormat = new DecimalFormat("¤0.00");
decimalFormat.setCurrency(this.currency);
String result = decimalFormat.format(amountDue);
return result;
}
So this was actually returning correctly formatted String but in the browser instead of, e.g. €99.99
I was getting ?99.99
- so instead of currency sign there was question mark displayed.
In the end, that was a problem with my Spring configuration, that I solved by adding characterEncoding
to my configuration:
<bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
...
<property name="characterEncoding" value="UTF-8" />
</bean>
you must have to define local instance over here..like I get Locale.ENGLISH
Double no=20.2563;
NumberFormat nf = NumberFormat.getNumberInstance(Locale.ENGLISH);
DecimalFormat df = (DecimalFormat)nf;
df.applyPattern("##.00");
String output = df.format(no);
© 2022 - 2024 — McMap. All rights reserved.