NSNumberFormatter 在 iPhone 4.0 中工作很奇怪
我正在开发一个应用程序,我需要在 3.0 和 4.0 中运行我的应用程序。我有一个文本字段,当我尝试在文本字段中输入数字时,行为是这样的...... 在 3.0 中:- 它允许输入 7 位数字和 2 个小数值(我已将其格式化为这样)。我根据所选国家/地区对数字以及逗号分隔进行了格式化和本地化。 中完美运行
它在 3.0 和 3.1.2 IN 4.0 :- 它只允许您输入 4 个数字,输入第 5 个数字后,它会使文本字段为空。当您输入第 5 个数字时,不会显示任何内容,而当您输入第 6 个数字时,它会从第一个数字开始,一直持续到 4 个数字。 例如: - 当您输入 1234 时,文本字段出现 - 1234,当您输入 12345 时,文本字段出现“ ”。当你现在输入 6 时,它会以 6 开头,依此类推。
我正在使用 NSNumberFormatter 和 numberfromstring 方法来格式化在文本字段中输入的值。
我无法理解为什么会这样......请帮助我......
I am developing an application i need to run my app both in 3.0 and 4.0. I have a textfield where when i try to enter numbers in the textfield the behaviour is like this...
IN 3.0 :-
It allows to enter 7 digits and 2 fractional values (I have formatted it like this). I have formatted and localized the numbers along with the comma seperations depending on the country selected. It is working perfectly in 3.0 and 3.1.2
IN 4.0 : -
It allows you to enter only 4 numbers and after entering 5th digit it is making the textfields empty.. Nothing is displayed when u enter the 5th number and when u enter the 6th number it starts from the 1st number and continues the same til 4 numbers.
ex: - when u enter 1234, textfield appears - 1234 and when u enter 12345, textfield appears " ". and when u enter 6 now it starts with 6 and so on..
I am using the NSNumberFormatter and numberfromstring method to format the values entered in the textfield.
I am not able to understand why this is happening like this... Please help me...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在本网站的另一个问题上给出了一些解决方法。据我所知,当文本包含空格或逗号(例如每 3 位数字)时,NSNumberFormatter 有时会出现问题。当它找到一个空间时,它返回 NIL。但是,在我的代码的另一个区域,它似乎工作正常
0
我遇到了同样的问题。我追踪到一个 NSNumberFormatter 语句,该语句不喜欢数字中每 3 位数字有一个空格(或逗号)。这是拥有数字格式化程序的原因之一。
NSNumber *number = [currencyFormatter numberFromString:mstring];
在互联网上的许多示例中,这是相当标准的代码,因此我怀疑很多人都会发现问题。
不管怎样,我通过去掉空格来修复它
NSArray *sArray = [mstring elementsSeparatedByString:@" "]; [mstring setString:@" "]; // 开头有空格就可以,最好是 nil for (NSString *sElement in sArray) { [mstringappendString:sElement];然后
,currencyFormatter 行就起作用了。
但是,在我的代码的另一个区域中,相同的currencyFormatter 语句没有任何问题。我尝试更改该区域的代码来导致问题,但我做不到。
所以,非常好奇!!!德里克 MakeItSoSudios.com
I gave some work-around on another question in this site. From what I can see the NSNumberFormatter sometimes has problems when the text has spaces or commas (eg every 3 digits). It returned NIL when it found a space. But, in another area of my code, it appeared to work OK
0
I had the same problem. I tracked it down to a NSNumberFormatter statement that did not like spaces (or commas) every 3 digits in the numbers. Which is one of the reasons for having a number formatter.
NSNumber *number = [currencyFormatter numberFromString:mstring];
It is fairly standard code in many examples on the internet, so I suspect a lot will find the problem.
Anyway, I fixed it by getting rid of the spaces
NSArray *sArray = [mstring componentsSeparatedByString:@" "]; [mstring setString:@" "]; //space at beginning is OK, would prefer nil for (NSString *sElement in sArray) { [mstring appendString:sElement]; }
Then the currencyFormatter line worked.
BUT, in another area of my code, that same currencyFormatter statement worked with no problem. I tried changing code in the area to cause the problem, but I could not.
So, very curious!!! Derek MakeItSoSudios.com