0

这是一个奇怪的输出,我似乎每次都会触发警报视图,而 for 的值num10.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];
}
4

2 回答 2

0
if (textField == self.textField1) {
      NSNumberFormatter *nFormatter = [[NSNumberFormatter alloc] init];
      [nFormatter setNumberStyle: NSNumberFormatterCurrencyStyle];
      self.textField1.text = [nFormatter stringFromNumber:[NSNumber numberWithFloat:     [self.textField1.text doubleValue] / 100]];
    }

您无法从货币中提取浮点数,因此 textField5 中的结果为零。因此将上述格式输出到相应的标签,以便用户可以看到格式化的字符串,如下所示。

if (textField == self.textField1) {
    NSNumberFormatter *nFormatter = [[NSNumberFormatter alloc] init];
    [nFormatter setNumberStyle: NSNumberFormatterCurrencyStyle];
    self.labeltest.text = [nFormatter stringFromNumber:[NSNumber numberWithFloat: [self.textField1.text doubleValue] /100]];
于 2013-05-13T19:19:58.407 回答
0

尝试在 NSNumber 中转换您的 textField,例如:

NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * num1 = [f numberFromString:self.textField1.text];
于 2013-05-13T11:26:21.363 回答