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