Re: Infinite or NaN in NSNumberFormatter doesn't work
Re: Infinite or NaN in NSNumberFormatter doesn't work
- Subject: Re: Infinite or NaN in NSNumberFormatter doesn't work
- From: Lars Sonchocky-Helldorf <email@hidden>
- Date: Mon, 29 Oct 2012 21:38:23 +0100
Hi Johann,
Am 29.10.2012 um 08:06 schrieb Johann Werner:
> Hi Lars,
>
> if I remember correctly the values NaN and ±Infinity are only defined for Float and Double. They don't exist for BigDecimal as you see in the code snippet that you posted: the constructor throws an NumberFormatException exception in that case.
I am not putting a BigDecimal into the formatter. It is actually a Double. If you check the decompiled code you see that NSNumberFormatter.stringForObjectValue() does that (in line 1026, where the Exception is thrown, in line 1025 is the check for Double):
/* */ public String stringForObjectValue(Object inNumber)
/* */ throws IllegalArgumentException
/* */ {
/* 1012 */ boolean isNegative = false;
/* */
/* 1015 */ if (inNumber == null)
/* 1016 */ return stringForNull();
/* */ BigDecimal tempNumber;
/* 1017 */ if (inNumber instanceof BigDecimal) {
/* 1018 */ if (inNumber == NSDecimalNotANumber) {
/* 1019 */ return stringForNotANumber();
/* */ }
/* 1021 */ tempNumber = (BigDecimal)inNumber;
/* */ }
/* */ else
/* */ {
/* */ BigDecimal tempNumber;
/* 1023 */ if (inNumber instanceof BigInteger) {
/* 1024 */ tempNumber = new BigDecimal((BigInteger)inNumber);
/* */ }
/* */ else
/* */ {
/* */ BigDecimal tempNumber;
/* 1025 */ if ((inNumber instanceof Double) || (inNumber instanceof Float)) {
/* 1026 */ tempNumber = new BigDecimal(((Number)inNumber).doubleValue());
/* */ }
I suppose that the code of BigDecimal must have changed over the years. Otherwise the code of lines 1017 to 1019 of NSNumberFormatter.stringForObjectValue() don't make any sense.
I hoped that there is maybe some fix in Wonder for this.
cheers,
Lars
>
> jw
>
>
> Am 25.10.2012 um 20:28 schrieb Lars Sonchocky-Helldorf <email@hidden>:
>
>> Hi list,
>>
>> I am trying to make use of http://api.webobjects.me/wo542/com/webobjects/foundation/NSNumberFormatter.html#setStringForNotANumber(java.lang.String) which oddly doesn't seem to work
>>
>> Here is my code for this:
>>
>> public synchronized NSNumberFormatter twoDecimalPlacesNumberFormatter()
>> {
>> if (twoDecimalPlacesNumberFormatter == null)
>> {
>> twoDecimalPlacesNumberFormatter = new NSNumberFormatter();
>> twoDecimalPlacesNumberFormatter.setPattern(",0.00");
>> // twoDecimalPlacesNumberFormatter.setLocalizesPattern(true);
>> twoDecimalPlacesNumberFormatter.setLocale(Locale.US);
>> twoDecimalPlacesNumberFormatter.setStringForNotANumber("-.--");
>> }
>>
>> return twoDecimalPlacesNumberFormatter;
>> }
>>
>> when trying to format a number like this:
>>
>> <td style = "border-right: 1px solid #666; text-align:right;"><wo:WOString value = "$currentAccumulatedPercentageOfTotalRevenueEntry" formatter = "$twoDecimalPlacesNumberFormatter" /> %</td>
>>
>> I get an empty String in the web page (instead of "-.--") and the following in the log:
>>
>> NumberFormatException: Infinite or NaN
>> at java.math.BigDecimal.<init>(BigDecimal.java:797)
>> at com.webobjects.foundation.NSNumberFormatter.stringForObjectValue(NSNumberFormatter.java:1026)
>> at com.webobjects.foundation.NSNumberFormatter.format(NSNumberFormatter.java:1654)
>> at java.text.Format.format(Format.java:140)
>>
>>
>> Then I tried to analyze what's going on by decompiling the code:
>>
>> BigDecimal:
>>
>> /* */ public BigDecimal(double paramDouble)
>> /* */ {
>> /* 796 */ if ((Double.isInfinite(paramDouble)) || (Double.isNaN(paramDouble))) {
>>
>> /* 797 */ throw new NumberFormatException("Infinite or NaN");
>>
>> /* */ }
>> /* */
>>
>> NSNumberFormatter:
>>
>> /* */ public String stringForObjectValue(Object inNumber)
>> /* */ throws IllegalArgumentException
>> /* */ {
>> /* 1012 */ boolean isNegative = false;
>> /* */
>> /* 1015 */ if (inNumber == null)
>> /* 1016 */ return stringForNull();
>> /* */ BigDecimal tempNumber;
>> /* 1017 */ if (inNumber instanceof BigDecimal) {
>> /* 1018 */ if (inNumber == NSDecimalNotANumber) {
>> /* 1019 */ return stringForNotANumber();
>> /* */ }
>> /* 1021 */ tempNumber = (BigDecimal)inNumber;
>> /* */ }
>> /* */ else
>> /* */ {
>> /* */ BigDecimal tempNumber;
>> /* 1023 */ if (inNumber instanceof BigInteger) {
>> /* 1024 */ tempNumber = new BigDecimal((BigInteger)inNumber);
>> /* */ }
>> /* */ else
>> /* */ {
>> /* */ BigDecimal tempNumber;
>> /* 1025 */ if ((inNumber instanceof Double) || (inNumber instanceof Float)) {
>>
>> /* 1026 */ tempNumber = new BigDecimal(((Number)inNumber).doubleValue());
>>
>> /* */ }
>> /* */ else
>> /* */ {
>> /* */ BigDecimal tempNumber;
>> /* 1027 */ if (inNumber instanceof Number) {
>> /* 1028 */ tempNumber = BigDecimal.valueOf(((Number)inNumber).longValue(), 0);
>> /* */ }
>> /* */ else
>> /* */ {
>> /* */ BigDecimal tempNumber;
>> /* 1029 */ if (inNumber instanceof Boolean)
>> /* 1030 */ tempNumber = BigDecimal.valueOf((((Boolean)inNumber).booleanValue()) ? 1L : 0L);
>> /* */ else
>> /* 1032 */ throw new IllegalArgumentException("NSNumberFormatter.stringForObjectValue: argument(" + inNumber + ") of type " + inNumber.getClass().getName() + " is not a Number");
>> /* */ }
>> /* */ }
>> /* */ }
>> /* */ }
>>
>>
>> so it looks like it is no longer possible to format NaNs using NSNumberFormatter?
>>
>> Does somebody know any ideas or solutions for this?
>>
>>
>> cheers,
>>
>> Lars
>>
>>
>> _______________________________________________
>> Do not post admin requests to the list. They will be ignored.
>> Webobjects-dev mailing list (email@hidden)
>> Help/Unsubscribe/Update your Subscription:
>>
>> This email sent to email@hidden
>>
>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden