0不工作在货币类型输入

时间:2022-04-26 16:31:06

I am taking the input in form of currency like 54 gets converted to 0.54,but When I am trying to enter 100, I get out as 0.1 only.The code is not working for 0. You cannot enter value as 100.00 .The code I am using is

我以货币的形式输入,比如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 个解决方案

#1


1  

Most of your initial "string cleaning" is wrong and your number formatter isn't correct. It should be something like this:

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

- (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;
}

If, for some reason, you want to use Decimal format instead of Currency format, make sure you set the minimum and maximum fraction digits (decimal places) to 2.

如果出于某种原因,您希望使用十进制格式而不是货币格式,请确保将最小和最大分数位数(小数点后几位)设置为2。

Do you really want to hardcode USD? What about people using the app in other countries?

你真的想硬编码USD吗?那么在其他国家使用该应用程序的人呢?

Your original string cleaning didn't properly support a user using cut, copy, or paste. It also used the wrong text to create cleanCentString.

原始的字符串清理不支持用户使用剪切、复制或粘贴。它还使用了错误的文本来创建cleanCentString。

#2


1  

What I understood is

我理解的是

If entered value is 0, you want 0.

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

If entered value is between 1 and 99, you want 0.01 to 0.99.

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

If entered value is 1 or more, you want 1.00, like wise.

如果输入的值是1或更多,则需要1.00,如wise。

Why don't you get straight as float requiredCurrency=inputCurrency/100.0f;

为什么不直接作为浮动要求货币=inputCurrency/100.0f;

#1


1  

Most of your initial "string cleaning" is wrong and your number formatter isn't correct. It should be something like this:

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

- (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;
}

If, for some reason, you want to use Decimal format instead of Currency format, make sure you set the minimum and maximum fraction digits (decimal places) to 2.

如果出于某种原因,您希望使用十进制格式而不是货币格式,请确保将最小和最大分数位数(小数点后几位)设置为2。

Do you really want to hardcode USD? What about people using the app in other countries?

你真的想硬编码USD吗?那么在其他国家使用该应用程序的人呢?

Your original string cleaning didn't properly support a user using cut, copy, or paste. It also used the wrong text to create cleanCentString.

原始的字符串清理不支持用户使用剪切、复制或粘贴。它还使用了错误的文本来创建cleanCentString。

#2


1  

What I understood is

我理解的是

If entered value is 0, you want 0.

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

If entered value is between 1 and 99, you want 0.01 to 0.99.

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

If entered value is 1 or more, you want 1.00, like wise.

如果输入的值是1或更多,则需要1.00,如wise。

Why don't you get straight as float requiredCurrency=inputCurrency/100.0f;

为什么不直接作为浮动要求货币=inputCurrency/100.0f;