我使用以下代码将文本字段中的转换后的输入数字格式化为显示USD货币。所以它显示$符号和每3位数字(从右侧)a被添加。我在文本字段中设置格式化文本。iOS从NSNumberFormatter获取值字符串
- (void)textFieldDidEndEditing:(UITextField *)textField
{
NSString *pureNumbers = [[textField.text componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""];
//Convert entered text to NSNumber
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * myNumber = [f numberFromString:pureNumbers];
NSString *currencyString = [currencyFormatter internationalCurrencySymbol];
NSString *format = [currencyFormatter positiveFormat];
format = [format stringByReplacingOccurrencesOfString:@"*" withString:currencyString];
// ¤ is a placeholder for the currency symbol
[currencyFormatter setPositiveFormat:format];
NSString *currencyString1 = [currencyFormatter stringFromNumber:myNumber];
NSLog(@"Text is %@",currencyString1);
textField.text = currencyString1;
}
现在我怎样才能得到后面输入的数字删除数字格式化程序?例如: 我在文本字段中输入了5000。它在文本字段中被格式化为5000美元。 我想从文本字段获取“5000”,这是用户输入的原始值。
提供示例,“实际输入的数字”含糊不清。 – zaph 2012-02-11 13:46:26
@CocoaFu,我提供了一个我期待的例子。 – Satyam 2012-02-11 13:50:12
这不正是你的代码已经做了什么,当它得到'myNumber'? – Tommy 2012-02-11 14:01:42