检索UITextfField值并转换为带小数的英寸?

时间:2011-06-10 14:48:12

标签: iphone objective-c xcode

如果我有像以下文本字段的格式:

//Formats the textfield based on the pickers.
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

NSString *result = [feetArray objectAtIndex:[feetPicker selectedRowInComponent:0]];

result = [result stringByAppendingFormat:@"%@ft", [feetArray objectAtIndex:[feetPicker selectedRowInComponent:1]]];
result = [result stringByAppendingFormat:@" %@", [inchArray objectAtIndex:[inchesPicker selectedRowInComponent:0]]];
result = [result stringByAppendingFormat:@"%@", [inchArray objectAtIndex:[inchesPicker selectedRowInComponent:1]]];
result = [result stringByAppendingFormat:@" %@in", [fractionArray objectAtIndex:[fractionPicker selectedRowInComponent:0]]];

myTextField.text = result;
}

00ft 00 0/16in这样的文本字段中的哪个显示?如何将所有显示更改为带小数的英寸?我需要取ft,然后乘以12 =变量。然后将其加到英寸,然后取我的1/16并将其除以16得到我的小数值,然后将其加到英寸所以它显示像1234.0625,以便进行我的计算。有人可以帮我完成这个吗?先感谢您!

2 个答案:

答案 0 :(得分:1)

NSString * theString = RiseTextField.text;
NSString * feetString = [theString substringWithRange:NSMakeRange(0, 2)];
NSString * inchesString = [theString substringWithRange:NSMakeRange(5, 2)];
NSUInteger rangeLength = ([theString length] == 14) ? 1 : 2;
NSString * fractionString = [theString substringWithRange:NSMakeRange(8, rangeLength)];

double totalInInches = [feetString doubleValue] * 12 + [inchesString doubleValue] + [fractionString doubleValue] / 16;

答案 1 :(得分:0)

您可以使用您在那里的数字进行计算,轻松获得所需的数字。获得实际数字后,应使用NSNumberFormatter为其提供所需的小数和格式。

这应该可以解决您的问题。或者您是否需要帮助将字符串转换为数字以便将它们一起添加?