iOS 从 NSNumberFormatter 字符串中获取值
我使用以下代码来转换文本字段中输入的数字,以将其格式化为显示美元货币。因此它显示 $ 符号,并且每 3 位数字(从右开始)添加 , 。我正在文本字段中设置格式化文本。
- (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”,这是用户输入的原始值。
I'm using following code to converted entered number in text field to be formatted to show USD Currency. So it shows the $ sign and for every 3 digits (from right) a , is added. I'm setting the formatted text in text field.
- (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;
}
Now how can I get the back the number entered removing the number formatter?
Example:
I entered 5000 in text field. Its formatted to $5,000 in the text field.
I want to get back "5000" from the text field which is the original value entered by user.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜您是从某个地方复制并粘贴了大部分格式化函数,而不是自己编写的,因为它已经包含执行此操作的代码。在第一行中,您说:
这需要一个字符串并删除所有非数字字符。所以这已经完全符合你的要求了。例如,如果您说
pureNumbers 字符串现在等于@“5000”。要将其转换为您可以实际进行数学运算的数值,您可以说:
I'm guessing you copied and pasted most of your formatting function from somewhere rather than writing it yourself, because it already contains the code to do this. In the first line you say:
This takes a string and strips out any non-numeric characters. So this already does exactly what you want. E.g. if you say
The pureNumbers string now equals @"5000". To convert that to numeric value that you can actually do math with, you can say:
您可以使用
UITextField
的leftView
属性:然后只需检索文本。无需转换或字符串操作。
You can use
leftView
property ofUITextField
:Then just retrieve the text. No conversion or strings operations needed.