FROM : j o a r
DATE : Tue Feb 26 23:10:57 2008
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
DATE : Tue Feb 26 23:10:57 2008
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
| Related mails | Author | Date |
|---|---|---|
| C Sandeep | Feb 26, 18:55 | |
| Randall Meadows | Feb 26, 19:11 | |
| Jean-Daniel Dupas | Feb 26, 19:12 | |
| Hamish Allan | Feb 26, 19:27 | |
| C Sandeep | Feb 26, 21:47 | |
| j o a r | Feb 26, 23:10 | |
| Graham | Feb 26, 23:42 |






Cocoa mail archive

