3

我以货币形式输入,例如 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;
}
4

2 回答 2

1

我的理解是

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

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

如果输入的值是 1 或更大,则同样需要 1.00。

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

于 2012-12-08T12:20:59.687 回答
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.

于 2012-12-08T17:08:57.963 回答