NSNumber 存储浮点数。 请问没有科学记数法吗?
我的代码看起来像这样,
NSNumber *inputToNumber = [NSNumber numberWithFloat:[textField.text floatValue]];
文本字段中的值实际上是一个电话号码。 它以烦人的 (2.0)78966e+08 的形式存储在 NSNumber 中,
我怎样才能让 NSNumber 将其存储为 0207896608?
My code looks like this
NSNumber *inputToNumber = [NSNumber numberWithFloat:[textField.text floatValue]];
the value from the textfield is actually a telephone number. It's stored in NSNumber as an annoying (2.0)78966e+08
How can I just get NSNumber to store it as 0207896608?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我认为将电话号码存储到 NSNumber 中的基本思想是有缺陷的:
我会使用 NSString 代替 NSNumber。
I think that the basic idea to store a phone number into a NSNumber is flawed:
I would use NSString in place of NSNumber.
仅仅因为它被称为数字并不意味着“电话号码”是与“5”或“pi”相同意义上的数字。
您应该将电话号码视为字符串,或者您应该创建一个 TelephoneNumber 模型类来表示每个电话号码。
Just because it's called a number doesn't mean a "telephone number" is a number in the same sense that "5" or "pi" are.
Either you should treat a telephone number as a string, or you should create a TelephoneNumber model class to represent each one.
考虑一下世界上有些地方的数字没有前导 0,并且有前导 0 的数字与没有前导 0 的相同数字不同。
所以别再偷懒了
NSNumber
破解并构建您自己的电话号码类。Consider that there are places in the world where numbers don't have leading 0's and where a number with a leading 0 is not the same as the same number without a leading 0.
So stop being lazy with that
NSNumber
hacks and build your own phone-number class.科学记数法在许多计算机语言中用作非常大(或非常小)数字的默认输出。 如果您希望数字以小数形式输出,则需要指定输出格式(实现因语言而异)。
此外,julesjacobs 是正确的。 您不应将 FLOAT 用于电话号码,因为它可能会出现二进制舍入错误。 使用 INT 或 STRING 将为您省去很多麻烦。
Scientific notation is used in may computer languages as the default output of very large (or very small) numbers. If you want the number to be output as a decimal, you need to specify the output format (the implementation varies by language.)
Also, julesjacobs is correct. You should not use FLOAT for a phone number as it is subject to binary rounding errors. Using INT or STRING will save you lots of headaches.
如果您需要能够将其作为数字处理,也许您应该将其分解为多个部分,并将每个部分存储为整数。
或者,如果不需要操作它,您可以将整个内容存储为字符串。
If you need to be able to deal with it as numbers maybe you should break it up into its parts, and store each part as an integer.
Or you could store the whole thing as a string if you don't need to manipulate it.
您是否将电话号码存储在浮点数中? 您应该考虑使用整数或字符串。 也许:
Are you storing a phone number in a float? You should consider using an integer or string. Perhaps:
嘿伙计们你觉得怎么样,这似乎满足了我的目的。 目前只有英国,所以当我有机会时我会担心本地化。
我用它来存储号码
,这个方法格式化我的电话号码并处理前面提到的 0。
感谢所有的评论。 我将把答案标记为建议 NSString 的人,因为我担心我会恢复使用 NSString 而不是我上面的解决方法。
Hey Guys what do you think of this, It seems to full-fill my purposes. Only UK at the moment so will worry about localization when I get a chance.
I use this to get to store the number
And this method formats my telephone number and takes care of the preceeding 0 mentioned.
Thanks for all the comments. I'm gonna mark the answer as whoever suggested NSString as I fear I will revert to using NSString for this instead of my above workaround.