編集:修正されました、これが私が将来の参照のためにそれをした方法です:
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桁を超えると小数が表示されないという問題があります。
次の図で説明されている問題:
<-動作していますが、小数点以下3桁に短縮されています。(そして、数学的に正しく行われる切り上げ)
そして問題:(右端の写真は正しい)
ご覧のとおり、下の画像は小数点を完全に無視しています。これを正しく機能させるには、どのコードを追加/変更する必要がありますか?
関連するコードは次のとおりです。
[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;
}
問題/修正はこのコードにあると思います。