【发布时间】:2012-02-11 13:32:29
【问题描述】:
我正在使用以下代码将文本字段中输入的数字转换为格式化以显示美元货币。所以它显示了 $ 符号,并且每 3 位数字(从右起)添加 a 。我正在文本字段中设置格式化文本。
- (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。它在文本字段中格式化为 5,000 美元。 我想从文本字段中取回“5000”,这是用户输入的原始值。
【问题讨论】:
-
提供示例,“输入的实际数字”含糊不清。
-
@CocoaFu,我提供了一个我所期待的例子。
-
当你的代码得到
myNumber时,这不正是你的代码已经在做的吗?
标签: ios nsnumberformatter