这是一个奇怪的输出,我似乎每次都会触发警报视图,而 for 的值num1
会0.50
很好。
当我点击计算时,警报字段被触发,并且num1
设置为0.00
,而tcost
总成本似乎总是0
。
这是以前从未发生过的。
输出的类型应该是诸如0.23
等等之类的东西。因为tcost
警报字段的原因是如果用户没有填写字段,我可以捕获。
(void)textFieldDidEndEditing:(UITextField *)textField
{
CGRect viewFrame = self.view.frame;
viewFrame.origin.y += animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
if (textField1) {
NSString *txt = self.textField1.text;
double num1 = [txt doubleValue];
double tCost = num1/100;
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle: NSNumberFormatterCurrencyStyle];
[numberFormatter setMaximumFractionDigits:3];
[numberFormatter setRoundingMode: NSNumberFormatterRoundUp];
NSString *numberString = [numberFormatter stringFromNumber:[NSNumber numberWithFloat:tCost]];
self.textField1.text = [NSString stringWithFormat:@"%@",numberString];
}
}
(IBAction)calculateCost:(UIButton *)sender {
NSString *rate = self.textField1.text;
NSString *wat = self.textField2.text;
NSString *hours = self.textField3.text;
NSString *Days = self.textField4.text;
long double num1 = [rate doubleValue];
long double num2 = [wat doubleValue];
long double num3 = [hours doubleValue];
long double num4 = [Days doubleValue];
//double num12 = num1 /10000;
double appKw = num2 / 1000;
double costKwph = appKw *num1;
double tCost = ((num4 * num3) * costKwph);
if (num2 == 0|| num1 <= 0.000|| num3 == 0 || num4 == 0) {
self.textField5.text = 0;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"oops" message:@"you must fill in all fields" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",nil];
[alert show];
}
tCost = tCost / 100;
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle: NSNumberFormatterCurrencyStyle];
[numberFormatter setMaximumFractionDigits:4];
NSString *numberAsString = [numberFormatter stringFromNumber:[NSNumber numberWithFloat:tCost]];
self.textField5.text = [NSString stringWithFormat:@"%@",numberAsString];
}