编辑:已修复,以下是我为未来参考做的事情:
NSNumber *inputNumber = [[NSNumber alloc ]initWithDouble:convertValue];
NSNumber *resultNumber = [[NSNumber alloc]initWithDouble:result];
NSNumberFormatter *formatterResult = [[NSNumberFormatter alloc] init];
formatterResult.numberStyle = NSNumberFormatterDecimalStyle;
NSNumberFormatter *formatterInput = [[NSNumberFormatter alloc] init];
formatterInput.numberStyle = NSNumberFormatterDecimalStyle;
[formatterResult setNumberStyle:NSNumberFormatterDecimalStyle];
[formatterResult setMaximumFractionDigits:6];
[formatterInput setNumberStyle:NSNumberFormatterDecimalStyle];
[formatterInput setMaximumFractionDigits:6];
//These four lines are the one fixing the issue.
NSString *formattedResultString = [formatterResult stringFromNumber:(NSNumber*)resultNumber];
NSString *formattedInputString = [formatterInput stringFromNumber:(NSNumber*)inputNumber];
NSString *formelString = [[NSString alloc]initWithFormat:
@" %@ %@ =", formattedInputString, convertFromName];
formelLabel.text = formelString;
NSString *resultString = [[NSString alloc]initWithFormat:
@" %@ %@",formattedResultString, convertToName];
resultLabel.text = resultString;
---------- ORIGINAL QUESTION ------------
所以我的NSNumberFormatter缩短数字太多了,而且当主号码超过8位时也没有显示小数。
下图中描述的问题:
< - 工作,但缩短为三位小数。 (并且向上舍入,这在数学上是正确的)
然后问题:(最右边的图片是正确的)
如您所见,底部图像完全忽略小数。我需要添加/更改哪些代码才能正常工作?
以下是相关代码:
[super viewDidLoad];
_convertFrom = @[@"MTPA", @"MMcf/day",
@"Mill.Sm3/day", @"MMBTU/day", @"Boe/day",@"ton LNG/day", @"GJ/day"];
_convertTo = @[@"MTPA", @"MMcf/day",
@"Mill.Sm3/day", @"MMBTU/day", @"Boe/day", @"ton LNG/day", @"GJ/day"];
_convertRates = @[ @1.0f, @133.3333333f, @3.775579545f,
@137333.3333f, @23747.68013, @1716.17252, @147247.6022];
//some place down in the code:
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
[self updateConversionLabel];
}
- (void)updateConversionLabel
{
float convertFrom = [[_convertRates objectAtIndex:[picker selectedRowInComponent:0]] floatValue];
float convertTo = [[_convertRates objectAtIndex:[picker selectedRowInComponent:1]] floatValue];
NSNumberFormatter *fmt = [NSNumberFormatter new];
float input = [fmt numberFromString:inputText.text].floatValue;
float to = convertTo;
float from = convertFrom;
float convertValue = input;
float relative = to / from;
float result = relative * convertValue;
NSString *convertFromName = [_convertFrom objectAtIndex:[picker selectedRowInComponent:0]];
NSString *convertToName = [_convertFrom objectAtIndex:[picker selectedRowInComponent:1]];
NSNumber *inputNumber = [[NSNumber alloc ]initWithFloat:convertValue];
NSNumber *resultNumber = [[NSNumber alloc]initWithFloat:result];
NSNumberFormatter *formatterResult = [[NSNumberFormatter alloc] init];
formatterResult.numberStyle = NSNumberFormatterDecimalStyle;
NSNumberFormatter *formatterInput = [[NSNumberFormatter alloc] init];
formatterInput.numberStyle = NSNumberFormatterDecimalStyle;
NSString *formattedResultString = [formatterResult stringFromNumber:(NSNumber*)resultNumber];
NSString *formattedInputString = [formatterInput stringFromNumber:(NSNumber*)inputNumber];
NSString *formelString = [[NSString alloc]initWithFormat:
@" %@ %@ =", formattedInputString, convertFromName];
formelLabel.text = formelString;
NSString *resultString = [[NSString alloc]initWithFormat:
@" %@ %@",formattedResultString, convertToName];
resultLabel.text = resultString;
}
我假设问题/修复在此代码中。
答案 0 :(得分:2)
编辑:已修复,以下是我今后的参考方式:
NSNumber *inputNumber = [[NSNumber alloc ]initWithDouble:convertValue];
NSNumber *resultNumber = [[NSNumber alloc]initWithDouble:result];
NSNumberFormatter *formatterResult = [[NSNumberFormatter alloc] init];
formatterResult.numberStyle = NSNumberFormatterDecimalStyle;
NSNumberFormatter *formatterInput = [[NSNumberFormatter alloc] init];
formatterInput.numberStyle = NSNumberFormatterDecimalStyle;
[formatterResult setNumberStyle:NSNumberFormatterDecimalStyle];
[formatterResult setMaximumFractionDigits:6];
[formatterInput setNumberStyle:NSNumberFormatterDecimalStyle];
[formatterInput setMaximumFractionDigits:6];
//These four lines are the one fixing the issue.
NSString *formattedResultString = [formatterResult stringFromNumber: (NSNumber*)resultNumber];
NSString *formattedInputString = [formatterInput stringFromNumber:(NSNumber*)inputNumber];
NSString *formelString = [[NSString alloc]initWithFormat:
@" %@ %@ =", formattedInputString, convertFromName];
formelLabel.text = formelString;
NSString *resultString = [[NSString alloc]initWithFormat:
@" %@ %@",formattedResultString, convertToName];
resultLabel.text = resultString;
答案 1 :(得分:1)
真正的算术是精确的。任何计算机算术都有限的范围 - 可以表示多少位数;如果由于我们人类使用十进制派和计算机使用二进制分数而导致分数不准确。
在您的代码中,您使用的是float
,它是一个32位二进制浮点数,精度约为6位十进制数,范围大致为10 ^ -38到10 ^ 38。您的号码最多为9位数。
尝试使用double
thoughtout(和doubleValue
等),这是一个64位二进制浮点,精度约为15位十进制数。您可能仍然发现数字没有按照您的意愿出现,为此您需要更多地了解如何格式化数字,但您应该获得所需的精确度。
如果您还需要更高的精度和十进制浮点数,请查看NSDecimalNumber。
有关详细信息,请参阅32-bit floating point和64-bit floating point。