0不适用于货币类型输入

时间:2012-12-08 11:11:13

标签: iphone objective-c ios

我以货币形式输入,如54转换为0.54,但当我尝试输入100时,我只得到0.1。代码不适用于0。您无法输入值100.00。我正在使用的代码是

 (BOOL)textField:(UITextField *)transactionAmount shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string
{

    NSString *substring = transactionAmount.text;
    substring = [substring stringByAppendingString:string];
    NSLog(@"Text : %@",substring);

    NSString *cleanCentString = [[transactionAmount.text
                                  componentsSeparatedByCharactersInSet:
                                  [[NSCharacterSet decimalDigitCharacterSet] invertedSet]]
                                 componentsJoinedByString:@""];
    // Parse final integer value
    NSInteger centAmount = cleanCentString.integerValue;
    // Check the user input
    if (string.length > 0)
    {
        // Digit added
        centAmount = centAmount * 10 + string.integerValue;
    }
    else
    {
        // Digit deleted
        centAmount = centAmount / 10;
    }
    // Update call amount value
    NSNumber *amount = [[NSNumber alloc] initWithFloat:(float)centAmount / 100.0f];
    // Write amount with currency symbols to the textfield
    NSNumberFormatter *_currencyFormatter = [[NSNumberFormatter alloc] init];
    // [_currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    [_currencyFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
    [_currencyFormatter setCurrencyCode:@"USD"];
    [_currencyFormatter setNegativeFormat:@"-¤#,##0.00"];
    self.transactionAmount.text = [_currencyFormatter stringFromNumber:amount];
  //  [self SetMainMessage:customTipsValue.text];


    return NO;
}

2 个答案:

答案 0 :(得分:1)

我理解的是

如果输入的值为0,则需要0。

如果输入的值介于1和99之间,则需要0.01到0.99。

如果输入的值为1或更多,则需要1.00,就像明智一样。

为什么不直截了当float requiredCurrency=inputCurrency/100.0f;

答案 1 :(得分:1)

您的大多数初始“字符串清理”都是错误的,您的数字格式化程序不正确。它应该是这样的:

- (BOOL)textField:(UITextField *)transactionAmount shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString *substring = transactionAmount.text;
    substring = [substring stringByReplacingCharactersInRange:range withString:string];
    NSLog(@"New Text : %@",substring);

    NSString *cleanCentString = [[substring
                              componentsSeparatedByCharactersInSet:
                              [[NSCharacterSet decimalDigitCharacterSet] invertedSet]]
                             componentsJoinedByString:@""];

    // Parse final integer value
    NSInteger centAmount = cleanCentString.integerValue;

    // Update call amount value
    NSNumber *amount = [[NSNumber alloc] initWithFloat:centAmount / 100.0f];

    // NOTE: make this an instance variable and set it up just once
    // Write amount with currency symbols to the textfield
    NSNumberFormatter *_currencyFormatter = [[NSNumberFormatter alloc] init];
    [_currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    [_currencyFormatter setCurrencyCode:@"USD"];
    [_currencyFormatter setNegativeFormat:@"-¤#,##0.00"];

    self.transactionAmount.text = [_currencyFormatter stringFromNumber:amount];

    return NO;
}

如果由于某种原因,您想要使用十进制格式而不是货币格式,请确保将最小和最大小数位(小数位)设置为2.

你真的想硬编码美元吗?那些在其他国家/地区使用该应用的用户呢?

原始字符串清理无法正确支持使用剪切,复制或粘贴的用户。它还使用了错误的文字来创建cleanCentString