Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/44.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 将字符串分数转换为十进制_Iphone_Objective C - Fatal编程技术网

Iphone 将字符串分数转换为十进制

Iphone 将字符串分数转换为十进制,iphone,objective-c,Iphone,Objective C,我有一个plist文件,我正在读取一个测量值,但有些测量值是分数,例如6 3/8“。我这样格式化它们,因为在卷尺上找到它比找到6.375更容易。”。我的问题是现在我想动态地转换成公制,而不是读取数字的小数部分。我现在的代码是这个 cutoutLabel.text = [NSString stringWithFormat:@"%.2f mm. %@", [[[sizeDict valueForKey:Sub_Size] objectForKey:@"Cutout Dimensions"]float

我有一个plist文件,我正在读取一个测量值,但有些测量值是分数,例如6 3/8“。我这样格式化它们,因为在卷尺上找到它比找到6.375更容易。”。我的问题是现在我想动态地转换成公制,而不是读取数字的小数部分。我现在的代码是这个

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

谢谢。

你可以得到如下分子和分母:

NSRange slashPos = [fraction.text rangeOfString:@"/"];

NSString * numerator = [fraction.text substringToIndex:slashPos.location];
NSString * denominator = [fraction.text substringFromIndex:slashPos.location+1];
你应该多加小心, 检查您的范围长度是否为1,并确保字符串在“/”字符之后有字符。但是,如果您知道您正在为该代码提供一个分数字符串,那么它应该在您的情况下工作


这个想法已经到位,但是你也需要先应用同样的逻辑来将整数和分数分开。应用同样的逻辑,搜索@“”,然后找到分子和分母,这就是我最后要做的

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

基于Ian的答案,并尝试更完整一点(因为他的示例是针对整数和带有英寸字符(6 3/8)的小数部分),我建议使用以下方法(如果整数前面有空格,也可以使用此方法):

// Convert a string consisting of a whole and fractional value into a decimal number
-(float) getFloatValueFromString: (NSString *) stringValue {

// The input string value has a format similar to 2 1/4". Need to remove the inch (") character and
// everything after it.
NSRange found = [stringValue rangeOfString: @"\""];    // look for the occurrence of the " character
if (found.location != NSNotFound) {
    // There is a " character. Get the substring up to the "\"" character
    stringValue = [stringValue substringToIndex: found.location];
}

// Now the input format looks something like 2 1/4. Need to convert this to a float value
NSArray *temp = [stringValue componentsSeparatedByString:@" "];
float convertedFraction = 0;
float wholeNumber = 0;
for (int i=0; i<[temp count]; i++) {
    if ([[temp objectAtIndex:i] isEqualToString:@""]) {
        continue;
    }
    NSArray *fraction = [[temp objectAtIndex:i]componentsSeparatedByString:@"/"];
    if ([fraction count] > 1) {
        convertedFraction = [[fraction objectAtIndex:0]floatValue]/[[fraction objectAtIndex:1]floatValue];
    }
    else if ([fraction count] == 1) {
        wholeNumber = [[fraction objectAtIndex:0] floatValue];
    }
}

convertedFraction += wholeNumber;

return convertedFraction;
}
//将由整数值和小数组成的字符串转换为十进制数
-(float)getFloatValueFromString:(NSString*)stringValue{
//输入字符串值的格式类似于2 1/4“。需要删除英寸(“)字符和
//之后的一切。
NSRange found=[stringValue rangeOfString:@“\”];//查找“字符”的出现处
if(found.location!=NSNotFound){
//有一个“字符。将子字符串设置为“\”字符
stringValue=[stringValue substringToIndex:found.location];
}
//现在输入格式看起来像2 1/4。需要将其转换为浮点值
NSArray*temp=[stringValue组件由字符串分隔:@”“];
浮点数转换分数=0;
浮点整数=0;
对于(int i=0;i 1){
convertedFraction=[[fraction objectAtIndex:0]浮点值]/[[fraction objectAtIndex:1]浮点值];
}
如果([分数计数]==1),则为else{
wholeNumber=[[分数对象索引:0]浮点值];
}
}
转换分数+=整数;
返回转换分数;
}

只需存储公制版本以及旧版和旧版。我希望避免这种情况,我需要在大约75个条目上进行转换。只需将它们输入谷歌。或者让实习生来做。这将花费你大约5到10分钟的时间。我想另一种选择是查找(或编写你自己的条目)库来解析分数,这将花费更长的时间。