我有一個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)上,鍵盤上有逗號,但逗號後的值始終爲零。
編輯:這工作
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];
,而不是setDecimalStyle,並設置diigits的數量等[嘗試[numberFormatter setPositiveFormat:@「### 0。##」]; – 2013-02-17 13:42:54