• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Infinite or NaN in NSNumberFormatter doesn't work
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Infinite or NaN in NSNumberFormatter doesn't work


  • Subject: Infinite or NaN in NSNumberFormatter doesn't work
  • From: Lars Sonchocky-Helldorf <email@hidden>
  • Date: Thu, 25 Oct 2012 20:28:29 +0200

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

  • Follow-Ups:
    • Re: Infinite or NaN in NSNumberFormatter doesn't work
      • From: Johann Werner <email@hidden>
  • Prev by Date: Re: Problems with H2 DB
  • Next by Date: Re: Migrations auto-run issue with shared Entities
  • Previous by thread: Re: centos 6.3 deploy still acting recalcitrantly
  • Next by thread: Re: Infinite or NaN in NSNumberFormatter doesn't work
  • Index(es):
    • Date
    • Thread