我是新手,我按照下面的代码设置我的计算,但我认为必须有一种方法可以只有一个NSNumberFormatter?
使用下面的方法,有时我会得到奇怪的结果,可能是因为某些计算使用了其他计算结果,任何帮助都非常赞赏。
谢谢你。
double VolumePerMtrLitres = ((3.141592654/4 * ([textField1.text doubleValue]/1000) * ([textField1.text doubleValue]/1000)) - (3.141592654/4 * ([textField2.text doubleValue]/1000) * ([textField2.text doubleValue]/1000))) * [textField5.text doubleValue] * 1000;
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setMaximumFractionDigits:0];
NSString *numberAsString = [formatter stringFromNumber: [NSNumber numberWithDouble:VolumePerMtrLitres]];
volumePerMeterLitreAnswer.text = [NSString stringWithFormat:@"%@ ltr.", numberAsString];
double LubricationPumpCapacity = [textField6.text doubleValue] / 1000 * [volumePerMeterLitreAnswer.text doubleValue];
NSNumberFormatter *formatter2 = [[NSNumberFormatter alloc] init];
[formatter2 setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter2 setMaximumFractionDigits:0];
NSString *numberAsString2 = [formatter2 stringFromNumber: [NSNumber numberWithDouble:LubricationPumpCapacity]];
pumpSpeedAnswer.text = [NSString stringWithFormat:@"%@ litre per min.", numberAsString2];
double VolumePerPipeLitre = [volumePerMeterLitreAnswer.text doubleValue] * [textField3.text doubleValue];
NSNumberFormatter *formatter3 = [[NSNumberFormatter alloc] init];
[formatter3 setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter3 setMaximumFractionDigits:0];
NSString *numberAsString3 = [formatter3 stringFromNumber: [NSNumber numberWithDouble:VolumePerPipeLitre]];
volumePerPipeLitreAnswer.text = [NSString stringWithFormat:@"%@ ltr.", numberAsString3];
double VolumeForDriveLitres = [volumePerMeterLitreAnswer.text doubleValue] * [textField4.text doubleValue];
NSNumberFormatter *formatter4 = [[NSNumberFormatter alloc] init];
[formatter4 setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter4 setMaximumFractionDigits:0];
NSString *numberAsString4 = [formatter4 stringFromNumber: [NSNumber numberWithDouble:VolumeForDriveLitres]];
volumeForDriveLitreAnswer.text = [NSString stringWithFormat:@"%@ ltr.", numberAsString4];
答案 0 :(得分:0)
您的代码可以简化为:
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setMaximumFractionDigits:0];
double text1 = [textField1.text doubleValue];
double text2 = [textField2.text doubleValue];
double text3 = [textField3.text doubleValue];
double text4 = [textField4.text doubleValue];
double text5 = [textField5.text doubleValue];
double text6 = [textField6.text doubleValue];
double VolumePerMtrLitres = ((M_PI_4 * (text1 / 1000) * (text1 / 1000)) - (M_PI_4 * (text2 / 1000) * (text2 / 1000))) * text5 * 1000;
NSString *numberAsString = [formatter stringFromNumber:@(VolumePerMtrLitres)];
volumePerMeterLitreAnswer.text = [NSString stringWithFormat:@"%@ ltr.", numberAsString];
double LubricationPumpCapacity = text5 / 1000 * [volumePerMeterLitreAnswer.text doubleValue];
NSString *numberAsString2 = [formatter stringFromNumber:@(LubricationPumpCapacity)];
pumpSpeedAnswer.text = [NSString stringWithFormat:@"%@ litre per min.", numberAsString2];
double VolumePerPipeLitre = [volumePerMeterLitreAnswer.text doubleValue] * text3;
NSString *numberAsString3 = [formatter stringFromNumber:@(VolumePerPipeLitre)];
volumePerPipeLitreAnswer.text = [NSString stringWithFormat:@"%@ ltr.", numberAsString3];
double VolumeForDriveLitres = [volumePerMeterLitreAnswer.text doubleValue] * text4;
NSString *numberAsString4 = [formatter stringFromNumber:@(VolumeForDriveLitres)];
volumeForDriveLitreAnswer.text = [NSString stringWithFormat:@"%@ ltr.", numberAsString4];
请注意M_PI_4
的用户替换您的3.14159 / 4
。另请注意使用@(someVariable)
语法而不是[NSNumber numberWithXXX:someNumber]
。
答案 1 :(得分:0)
对于那些感兴趣的人,因为我的上面的计算被格式化为在值达到一千(我想要的)之后包含一个逗号,由于使用其他计算的结果(已经用逗号格式化)的一些计算,我得到了奇怪的结果) 我能让它正常工作的唯一方法是“不”使用其他计算的结果,而是单独进行所有计算,这意味着重复相同的计算几次,但它现在完美。 ;) 这是我最终使用的代码,感谢maddy的帮助。
double machineOD = [textField1.text doubleValue];
double pipeOD = [textField2.text doubleValue];
double pipeLength = [textField3.text doubleValue];
double driveLength = [textField4.text doubleValue];
double muckUpFactor = [textField5.text doubleValue];
double jackingSpeed = [textField6.text doubleValue];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setMaximumFractionDigits:0];
double VolumePerMtrLitres = ((M_PI_4 * (machineOD / 1000) * (machineOD / 1000)) - (M_PI_4 * (pipeOD / 1000) * (pipeOD / 1000))) * muckUpFactor * 1000;
NSString *numberAsString = [formatter stringFromNumber:@(VolumePerMtrLitres)];
volumePerMeterLitreAnswer.text = [NSString stringWithFormat:@"%@ ltr.", numberAsString];
double LubricationPumpCapacity = jackingSpeed / 1000 * ((M_PI_4 * (machineOD / 1000) * (machineOD / 1000)) - (M_PI_4 * (pipeOD / 1000) * (pipeOD / 1000))) * muckUpFactor * 1000;
NSString *numberAsString2 = [formatter stringFromNumber:@(LubricationPumpCapacity)];
pumpSpeedAnswer.text = [NSString stringWithFormat:@"%@ ltr per min.", numberAsString2];
double VolumePerPipeLitre = ((M_PI_4 * (machineOD / 1000) * (machineOD / 1000)) - (M_PI_4 * (pipeOD / 1000) * (pipeOD / 1000))) * muckUpFactor * 1000 * pipeLength;
NSString *numberAsString3 = [formatter stringFromNumber:@(VolumePerPipeLitre)];
volumePerPipeLitreAnswer.text = [NSString stringWithFormat:@"%@ ltr.", numberAsString3];
double VolumeForDriveLitres = ((M_PI_4 * (machineOD / 1000) * (machineOD / 1000)) - (M_PI_4 * (pipeOD / 1000) * (pipeOD / 1000))) * muckUpFactor * 1000 * driveLength;
NSString *numberAsString4 = [formatter stringFromNumber:@(VolumeForDriveLitres)];
volumeForDriveLitreAnswer.text = [NSString stringWithFormat:@"%@ ltr.", numberAsString4];