如何将多个doubleValues合并为一个新的double?目前我尝试过这样:
double sum = doubleWantToSum1 + doubleWantToSum2 + doubleWantToSum; //etc...
此外,我想在
中使用此等式的结果double tooMuch = sum - 100;
我想要返回100减去我的花车的总价值。目前唯一能做的就是输出doubleWantToSum1 - 100.我相信这个简单的答案非常简单,但由于我在互联网上找不到任何关于此的内容,如果你们其中一个人告诉我,我会很高兴。
编辑,添加代码 请注意,我知道这很差,可能也无效。
//Nummerformatterere - Kinda unødvendig
NSNumberFormatter *fmt1 = [NSNumberFormatter new]; //Temp
NSNumberFormatter *fmt2 = [NSNumberFormatter new]; //Methane
NSNumberFormatter *fmt3 = [NSNumberFormatter new]; //Ethane
NSNumberFormatter *fmt4 = [NSNumberFormatter new]; //Propane
NSNumberFormatter *fmt5 = [NSNumberFormatter new]; //n-butane
NSNumberFormatter *fmt6 = [NSNumberFormatter new]; //i-butane
NSNumberFormatter *fmt7 = [NSNumberFormatter new]; //n-petane
NSNumberFormatter *fmt8 = [NSNumberFormatter new]; //i-petane
NSNumberFormatter *fmt9 = [NSNumberFormatter new]; //n-hexane
NSNumberFormatter *fmt10 = [NSNumberFormatter new]; //nitrogen
NSNumberFormatter *fmt11 = [NSNumberFormatter new]; //oxygen
//Float for å få textfieldsene til nummer
double temprature = [fmt1 numberFromString: tempratureText.text].doubleValue;
double methane = [fmt2 numberFromString: methaneText.text ].doubleValue;
double ethane = [fmt3 numberFromString: ethaneText.text ].doubleValue;
double propane = [fmt4 numberFromString: propaneText.text ].doubleValue;
double nbutane = [fmt5 numberFromString: nbutaneText.text ].doubleValue;
double ibutane = [fmt6 numberFromString: ibutaneText.text ].doubleValue;
double npetane = [fmt7 numberFromString: npetaneText.text ].doubleValue;
double ipetane = [fmt8 numberFromString: ipetaneText.text ].doubleValue;
double nhexane = [fmt9 numberFromString: nhexaneText.text ].doubleValue;
double nitrogen = [fmt10 numberFromString: nitrogenText.text ].doubleValue;
double oxygen = [fmt11 numberFromString: oxygenText.text ].doubleValue;
//Floats og formatters for underflow, overflow
double sum = (methane + ethane + propane + nbutane + ibutane + npetane + ipetane + nhexane + nitrogen + oxygen);
double overflowPart = 100 - sum;
double underflowPart = sum - 100;
NSNumber *underFlow = [[NSNumber alloc]initWithDouble:underflowPart];
NSNumberFormatter *underFlowResult = [[NSNumberFormatter alloc] init];
underFlowResult.numberStyle = NSNumberFormatterDecimalStyle;
[underFlowResult setNumberStyle:NSNumberFormatterDecimalStyle];
[underFlowResult setMaximumFractionDigits:2];
NSString *formattedUnderFlow = [underFlowResult stringFromNumber:(NSNumber*)underFlow];
NSNumber *overFlow = [[NSNumber alloc]initWithDouble:underflowPart];
NSNumberFormatter *overFlowResult = [[NSNumberFormatter alloc] init];
overFlowResult.numberStyle = NSNumberFormatterDecimalStyle;
[overFlowResult setNumberStyle:NSNumberFormatterDecimalStyle];
[overFlowResult setMaximumFractionDigits:2];
NSString *formattedOverFlow = [overFlowResult stringFromNumber:(NSNumber*)overFlow];
//Floats for variabler til ligningen
double Xi = 1;
double Mi = 2;
double Vi = 3;
double Vc = 4;
//Ligningen for å regne ut density
double over1 = Xi * Mi;
double under1 = Xi * Vi - Vc;
//=RESULT=
double result = over1 / under1;
/*----------STREK FORDI DET ER CHILL OG JEG ER FERDIG MED FLOATER----------*/
NSString *resultString = [[NSString alloc]initWithFormat:@"D = %f", result];
NSString *overflowString = [[NSString alloc]initWithFormat:@"Total %@ greater than 100 %", formattedOverFlow ];
NSString *underflowString = [[NSString alloc]initWithFormat:@"Total %@ less than 100 %", formattedUnderFlow];
/*----------STREK FORDI DET ER CHILL OG JEG ER FERDIG MED STRINGS----------*/
/*
if (temprature == 0 || methane == 0 || ethane == 0 || propane == 0 || nbutane == 0 || ibutane == 0 || oxygen == 0 || npetane == 0 || ipetane == 0 || nhexane == 0 || nitrogen == 0)
{
outputText.text = @"Please enter all values";
} */
if (underflowPart == 100)
{
outputText.text = @"Start by entering values above";
}
if (sum == 100)
{
outputText.text = resultString;
}
if (sum > 100)
{
outputText.text = overflowString;
}
if (sum < 100)
{
outputText.text = underflowString;
}
/*----------STREK FORDI DET ER CHILL OG JEG ER FERDIG MED 'if'----------*/
答案 0 :(得分:0)
这可能取决于您添加的数字的大小(精确度)。为了解决这个问题,你可以使用NSDecimalNumber,它最多可以包含38位十进制数字。
答案 1 :(得分:0)
评论中尚未明确说明您对代码中数字求和的方式是正确的。评论所暗示的是你总结的数字是错误的,你应该记录它们以查看它们是什么。
同样,您可以使用以下方法保存一些代码(消除数字格式化程序):
double temprature = [tempratureText.text doubleValue];
而不是
double temprature = [fmt1 numberFromString: tempratureText.text].doubleValue;