• 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
Re: API to convert string to number?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: API to convert string to number?


  • Subject: Re: API to convert string to number?
  • From: "I. Savant" <email@hidden>
  • Date: Tue, 27 Feb 2007 19:34:39 -0500

On Feb 27, 2007, at 7:21 PM, E. Wing wrote:
The string isn't coming from a standard Cocoa/UI widget so the string
is unsanitized. So I don't know if I can get NSNumberFormatter into
the picture. Any ideas?


If you read the docs as has been suggested, you'll see that NSNumberFormatter (in 10.4) has a method -numberFromString: which, according to the docs, "Returns an NSNumber object created by parsing a given string."

If you're targeting <10.4, then NSNumberFormatter (as a subclass of NSFormatter) also responds to - getObjectValue:forString:errorDescription:

... so, what you can do is instantiate your own NSNumberFormatter, set up its format, and try to - getObjectValue:forString:errorDescription: ... if it fails, hand the error description back to the user. It doesn't have to be attached to a text field to work.

Note, I have not done this, but this is the first thing I'd try before anything else. It's simplest since NSNumberFormatter already has everything you need and reinventing the wheel is seldom the best approach.

--
I.S.


On Feb 27, 2007, at 7:21 PM, E. Wing wrote:

The string isn't coming from a standard Cocoa/UI widget so the string
is unsanitized. So I don't know if I can get NSNumberFormatter into
the picture. Any ideas?

Thanks,
Eric


On 2/27/07, I. Savant <email@hidden> wrote:

In this particular case, an NSNumberFormatter attached to your field in Interface Builder will take care of this automatically. Read the documentation for more details.

--
I.S.


On Feb 27, 2007, at 7:08 PM, E. Wing wrote:

> So NSScanner seems to be the closest to what I need. But it fails to
> respond to the NSThousandsSeparator. Looking over the documentation,
> there is only mention of the decimal separator. Is it possible to get
> it to handle the other separators?
>
> Basically, what I want is when the user types 1,000.50 (in en- US), I
> want the value 1000.5. But I actually get 1.0 in this case.
>
> Here is the code I'm using:
> float converted_value;
> NSScanner* the_scanner = [NSScanner
> localizedScannerWithString:user_string];
> BOOL did_find_number = [the_scanner scanFloat:&converted_value];
>
> Thanks,
> Eric
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
> 40gmail.com
>
> This email sent to email@hidden




_______________________________________________

Cocoa-dev mailing list (email@hidden)

Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: API to convert string to number?
      • From: "E. Wing" <email@hidden>
References: 
 >API to convert string to number? (From: "E. Wing" <email@hidden>)
 >Re: API to convert string to number? (From: "Shawn Erickson" <email@hidden>)
 >Re: API to convert string to number? (From: "E. Wing" <email@hidden>)
 >Re: API to convert string to number? (From: "I. Savant" <email@hidden>)
 >Re: API to convert string to number? (From: "E. Wing" <email@hidden>)

  • Prev by Date: An idea about desktop amusing
  • Next by Date: How to launch an installer(.pkg) programmatically in cocoa?
  • Previous by thread: Re: API to convert string to number?
  • Next by thread: Re: API to convert string to number?
  • Index(es):
    • Date
    • Thread