1

编辑:已修复,这是我如何做以供将来参考:

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;

----------原始问题------------

所以我有一个 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;


}

我假设问题/修复在此代码中。

4

2 回答 2

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;
于 2012-10-30T18:22:28.173 回答
1

真正的算术是精确的。任何计算机算术都有有限的范围——可以表示多少位;在分数不准确的情况下,由于我们人类使用小数派系和计算机使用二进制分数。

在您使用的代码中,float它是一个 32 位二进制浮点数,精度约为 6 位十进制数字,范围大致从 10^-38 到 10^38。您的号码最多为 9 位数字。

尝试使用doublethoughtout(doubleValue等等),它是一个 64 位二进制浮点,精度约为 15 个十进制数字。您可能仍然会发现数字并不如您所愿,为此您需要更多地研究如何格式化数字,但您应该获得您所追求的精度。

如果您需要更高的精度和十进制浮点数,请查看NSDecimalNumber

有关详细信息,请参阅32 位浮点64 位浮点

于 2012-10-24T19:00:33.183 回答