2011-06-10 16 views
0

如果我已經格式化像一個文本框:檢索UITextfField值並將其轉換爲帶有小數的英寸?

//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的文本框顯示的我怎樣才能改變,所有與十進制英寸?我需要把英尺乘以12 =變量。然後將其加到英寸,以及將我的分數1/16除以16得到我的小數值,然後將其加到英寸中,顯示爲1234.0625,以便進行計算。有人能幫我完成這件事嗎?先謝謝你!

回答

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; 
+0

現在我得到的文本字段的值是這樣的'double a = [[RiseTextField text] doubleValue];'我猜我需要一些如何調用'totalInInchesString'? – Jason 2011-06-14 13:27:33

+0

像這樣:'double a = [totalInInchesString floatValue];'? – Jason 2011-06-14 13:47:10

+0

似乎並沒有將分數分解...... – Jason 2011-06-14 14:02:16

0

您可以通過使用您在那裏的數字進行計算來輕鬆獲取所需的數字。一旦你得到了實際的數字,你應該使用一個NSNumberFormatter來呈現所需的小數位數和格式。

這應該可以解決您的問題。或者您是否需要幫助將字符串轉換爲數字,以便將它們添加到一起?

+0

嗯,我orginally有它在那裏我會在文本框輸入十進制值,我不喜歡這一點,所以我改變了輸入拾荒者,所以我可以讓用戶只輸入了什麼,我想他們,而不是格式化我自己的鍵盤。所以我已經有一個函數在計算後將小數轉換爲小數,但現在我需要在計算之前將小數轉換爲小數。所以我認爲我真的需要用十進制來隱藏所有的英寸,聲音是否正確? – Jason 2011-06-10 16:52:35

+0

或者你可以用我的'00ft 00 0/16in'在NSNumberFormatter上給出一個例子嗎? – Jason 2011-06-13 13:13:10

相關問題