2013-02-17 76 views
2

我有一個UITextField,使用此代碼,打印底部標籤上的值並格式化它。NSNumberFormatter不同於設備和模擬器

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; 
[formatter setNumberStyle:NSNumberFormatterDecimalStyle]; 
[formatter setUsesGroupingSeparator:NO]; 
[formatter setMaximumFractionDigits:2]; 
[formatter setMinimumFractionDigits:2]; 
NSNumber *calcolo = [NSNumber numberWithDouble:[value1 doubleValue]-[self.textfield.text doubleValue]]; 
formattedString = [formatter stringFromNumber:calcolo]; 
self.label.text = formattedString; 

在模擬器(美國)上,數值顯示正確。在設備(IT)上,鍵盤上有逗號,但逗號後的值始終爲零。 enter image description here

編輯:這工作

NSNumber *temporaryValue1 = [formatter numberFromString:[NSString stringWithFormat:@"%@",value1]]; 
NSNumber *temporaryTextField = [formatter numberFromString:self.textField.text]; 
NSNumber *calcolo = [NSNumber numberWithFloat:([temporaryValue1 floatValue] - [temporaryTextField floatValue])]; 
formattedString = [formatter stringFromNumber:calcolo]; 
+0

,而不是setDecimalStyle,並設置diigits的數量等[嘗試[numberFormatter setPositiveFormat:@「### 0。##」]; – 2013-02-17 13:42:54

回答

1

我不認爲這個問題是你的輸出。問題是你從-[NSString doubleValue得到的輸入]。該方法不尊重用戶的區域設置。

您應該可以使用您製作的格式化程序將self.textField.text轉換爲適當的區域設置。或者你可以考慮使用NSDecimalNumber,特別是如果這是貨幣(看起來像?)。然後NSDecimalNumber有一個方法+decimalNumberWithString它也適合你。

希望有所幫助。

+0

謝謝!很棒!我在我的問題上添加了工作代碼 – Vins 2013-02-17 14:42:17

+0

@Vins這就是我在回答你的早期問題時所要做的 - http://stackoverflow.com/questions/14913269/ios-replace-char-in-uitextfield – rmaddy 2013-02-17 16:08:34

+0

@rmaddy是的,其實我已經聽取了你的建議:) – Vins 2013-02-17 17:29:52

相關問題