bug in floatFromMixedNumberString

0 views
Skip to first unread message

Jason Martens

unread,
May 12, 2009, 12:35:46 AM5/12/09
to rtfm-development-...@googlegroups.com
There is a bug in the floatFromMixedNumberString function.  It turns out (after much pain!) that some of the amount strings have a space after the last number, causing this function to return infinity when it should return a value.  To see the problem, imagine what happens when it receives a "5 ".

To fix it, add the second line below, and modify the (former second) third line to use the trimmedString like below.

Jason


float floatFromMixedNumberString(NSString *str) {
NSString * trimmedString = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSArray *parts = [trimmedString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" /"]];

Patrick Wong

unread,
May 12, 2009, 12:40:10 AM5/12/09
to rtfm-development-...@googlegroups.com
I don't think that works, wouldn't that make a string like "2 3/4" "23/4" ?

Try this (sorry it's in pseudocode, I don't have the source file with me):

case <number of parts == 2>
  if ([[parts objectAtIndex:1] isEqualToString:@"")
     return [parts objectAtIndex: 0];
  else
    <code as usual>

-Patrick

2009/5/11 Jason Martens <m...@jasonmartens.com>



--
Patrick Wong
Northwestern University, 2009
McCormick School of Engineering and Applied Sciences
Computer Science
(224) 522-1638

Jason Martens

unread,
May 12, 2009, 1:49:01 AM5/12/09
to rtfm-development-...@googlegroups.com
It works, trust me. :)


From: Patrick Wong
Date: Mon, 11 May 2009 23:40:10 -0500
To: <rtfm-development-...@googlegroups.com>
Subject: Re: bug in floatFromMixedNumberString

Nate

unread,
May 12, 2009, 11:13:22 AM5/12/09
to RTFM: Development for Mobile Devices
Yeah, I think this is the same issue that was causing Ben's null
strips of bacon, sorry about that.

Patrick, "trim" and "strip" string methods usually just operate on the
ends.

Nate

On May 12, 12:49 am, "Jason Martens" <m...@jasonmartens.com> wrote:
> It works, trust me.  :)
>
>
>
> -----Original Message-----
> From: Patrick Wong <patr...@u.northwestern.edu>
>
> Date: Mon, 11 May 2009 23:40:10

Nate

unread,
May 12, 2009, 12:28:18 PM5/12/09
to RTFM: Development for Mobile Devices
I updated the code in the MixedNumbers project to incorporate Jason's
fix (thanks.)

You can download it from the Recipes page: http://eecs395.ndnichols.com/recipe.php

Nate
Reply all
Reply to author
Forward
0 new messages