NSString floatValue issue

  • Greetings,
    I need to evaluate a NSString as a float value (represented by
    NSNumber object). Here is my code snippet:

    ----
        NSString *str = @"28/5";
        NSNumber *nm = [NSNumber numberWithFloat:[str floatValue]];
        NSLog(@"number: %@", nm );
    ----

    this results in:

    ----
    number: 28.0
    ----

    However, I was expecting 5.6 as the result. How do I achieve this ? Thanks.

    - Sandeep
  • On Feb 26, 2008, at 10:55 AM, C Sandeep wrote:

    > Greetings,
    > I need to evaluate a NSString as a float value (represented by
    > NSNumber object). Here is my code snippet:
    > ----
    > NSString *str = @"28/5";
    > NSNumber *nm = [NSNumber numberWithFloat:[str floatValue]];
    > NSLog(@"number: %@", nm );
    > ----
    > this results in:
    > ----
    > number: 28.0

    Right.  NSNumber won't evaluate the arithmetic expression, it'll only
    convert a string representation of a number, stopping when it finds
    something that's not part of a number (in this case, the "/").  You'll
    need to do the evaluating yourself.
  • As far as I know, there is no Math expression parser buit-in Cocoa.
    You will have to write your own evaluator to do this.

    Le 26 févr. 08 à 18:55, C Sandeep a écrit :

    > Greetings,
    > I need to evaluate a NSString as a float value (represented by
    > NSNumber object). Here is my code snippet:
    >
    > ----
    > NSString *str = @"28/5";
    > NSNumber *nm = [NSNumber numberWithFloat:[str floatValue]];
    > NSLog(@"number: %@", nm );
    > ----
    >
    > this results in:
    >
    > ----
    > number: 28.0
    > ----
    >
    > However, I was expecting 5.6 as the result. How do I achieve this ?
    > Thanks.
    >
    > - Sandeep
    >
  • On Tue, Feb 26, 2008 at 6:12 PM, Jean-Daniel Dupas
    <devlists...> wrote:

    > As far as I know, there is no Math expression parser buit-in Cocoa.
    > You will have to write your own evaluator to do this.

    Or use e.g. NSTask with 'bc' (remembering to specify a scale for bc).

    Hamish
  • Hi all,

    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;
    }

    - Sandeep

    On Tue, Feb 26, 2008 at 1:27 PM, Hamish Allan <hamish...> wrote:
    > On Tue, Feb 26, 2008 at 6:12 PM, Jean-Daniel Dupas
    > <devlists...> wrote:
    >
    >> As far as I know, there is no Math expression parser buit-in Cocoa.
    >> You will have to write your own evaluator to do this.
    >
    > Or use e.g. NSTask with 'bc' (remembering to specify a scale for bc).
    >
    > Hamish
    >
  • 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
  • [NSString floatValue] won't evaluate expressions, only convert string
    representations of numbers.

    I have written a category on NSString that does evaluate expressions
    however; you can find it here: http://apptree.net/parser.htm

    HTH,

    --------
    S.O.S.

    On 27/02/2008, at 4:55 AM, C Sandeep wrote:

    > However, I was expecting 5.6 as the result. How do I achieve
    > this ? Thanks.
    >
    > - Sandeep
previous month february 2008 next month
MTWTFSS
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29    
Go to today
MindNode
MindNode offered a free license !