Ian Oswald Ian Oswald - 9 months ago 87
Objective-C Question

Convert string fraction to decimal

I have a plist file that I am reading out a measurement, but some of the measurements are fractions such as 6 3/8". I formatted them that way because it's easier to find that on a tape measure than it is to find 6.375". My problem is now I want to do a conversion to metric on the fly and it isn't reading in the fraction part of the number. My current code is this.

cutoutLabel.text = [NSString stringWithFormat:@"%.2f mm. %@", [[[sizeDict valueForKey:Sub_Size] objectForKey:@"Cutout Dimensions"]floatValue] * 25.4, [temp objectAtIndex:2]];


Answer Source

That's what I ended up doing.

NSArray *temp = [[[sizeDict valueForKey:Sub_Size] objectForKey:@"Cutout Dimensions"] componentsSeparatedByString:@" "];
        if ([temp count] > 2) {
            NSArray *fraction = [[temp objectAtIndex:1]componentsSeparatedByString:@"/"];
            convertedFraction = [[fraction objectAtIndex:0]floatValue]/[[fraction objectAtIndex:1]floatValue];