Re: API to convert string to number?
Re: API to convert string to number?
- Subject: Re: API to convert string to number?
- From: "E. Wing" <email@hidden>
- Date: Tue, 27 Feb 2007 17:55:21 -0800
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."
So I relooked at NSNumberFormatter. It handles the thousands separator
correctly, but it fails to handle exponent characters. (strtod and
NSScanner handle the exponent but fail with the thousands separator)
I tried using the method setExponentSymbol, but it has no effect. In
fact, if I call exponentSymbol right after the set to check the value,
I get nil. So what did I screw up now?
float converted_value;
NSNumberFormatter* number_formatter = [[[NSNumberFormatter alloc]
init] autorelease];
[number_formatter setExponentSymbol:@"e"];
NSLog(@"exponentSymbol: %@", [number_formatter exponentSymbol]);
NSNumber* ns_number = [number_formatter numberFromString:user_string];
if(nil == ns_number)
{
NSLog(@"numberFromString failed");
return;
}
converted_value = [ns_number floatValue];
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:
This email sent to email@hidden