• 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: NSString floatValue issue
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSString floatValue issue


  • Subject: Re: NSString floatValue issue
  • From: j o a r <email@hidden>
  • Date: Tue, 26 Feb 2008 23:10:57 +0100


On Feb 26, 2008, at 9:47 PM, C Sandeep wrote:

Thanks for the ideas. Im using the class method to evaluate such
strings, thusly:

-(NSNumber *) getNumberFrom: (NSString *) str {

   NSScanner *scanner = [NSScanner scannerWithString: str];
   int numerator, denominator;
   [scanner scanInt: &numerator];
   [scanner scanString:@"/" intoString: NULL];
   [scanner scanInt: &denominator];
   float result = (float) numerator/denominator;
   NSNumber *nm = [NSNumber numberWithFloat: result];

   return nm;
}


Unless you have very good control over the input to your function, you should probably add better error checking:

	if ([scanner scanInt: &numerator])
	{
		// Continue parsing
	}

j o a r


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please 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


References: 
 >NSString floatValue issue (From: "C Sandeep" <email@hidden>)
 >Re: NSString floatValue issue (From: Jean-Daniel Dupas <email@hidden>)
 >Re: NSString floatValue issue (From: "Hamish Allan" <email@hidden>)
 >Re: NSString floatValue issue (From: "C Sandeep" <email@hidden>)

  • Prev by Date: Can CoreData return only unique results of an attribute
  • Next by Date: Re: Observing with GC
  • Previous by thread: Re: NSString floatValue issue
  • Next by thread: Re: NSString floatValue issue
  • Index(es):
    • Date
    • Thread