[英]Shouldn't a long be big enough to be able to store a phone number?
When setting a mobile phone number variable using long or float it says 'Error: integer number to large: 07859664443' 使用long或float设置手机号码变量时,它会显示“错误:整数到大:07859664443”
Here's the code 这是代码
public class StudentNode
{
public long TelNumber;
public void setTelNumber(long aTelNumber)
{
TelNumber = aTelNumber;
}
}
Shouldn't a long be big enough to be able to store a phone number? 长度不应该足够大以能够存储电话号码吗?
Telephone numbers aren't numbers. 电话号码不是号码。 They're strings.
他们是字符串。 For instance, in the UK and many other European countries, the first digit of a full phone number is a 0. But a simple numeric type like
long
has no way to indicate that a leading 0 is significant, the number 07859 664 443 would be the value 7859664443
. 例如,在英国和许多其他欧洲国家/地区,完整电话号码的第一位数字为0。但是像
long
这样的简单数字类型却无法表示前导0是有效数字,因此数字07859 664 443将为值7859664443
。 The leading zero matters. 前导零很重要。 Separately, it's not at all uncommon to see a number written like this: +44 (0)7859 664 443. Put that in your
long
and try to smoke it. 另外,看到这样写的数字并不少见:+44(0)7859 664443。将它放
long
然后尝试抽烟。 :-) :-)
Also, the formatting matters. 同样,格式很重要。 07859 664 443 is a lot easier to read and dial than 07859664443. In the US, they frequently put area codes in parentheses: (800) 123-4567, which again is easier for we poor humans to deal with than 8001234567.
07859 664 443比07859664443更容易阅读和拨打。在美国,他们经常在区号中加上(800)123-4567,与8001234567相比,这对我们的穷人来说更容易处理。
In my experience, the best way to deal with phone numbers is to store them as strings, and largely to leave them the way they were entered, unless you want to limit the application to the phone numbers used in a very limited geographic area — and even then, things can change. 以我的经验,处理电话号码的最佳方法是将它们存储为字符串,并且很大程度上保留它们的输入方式,除非您希望将应用程序限制为在非常有限的地理区域中使用的电话号码-并且即使那样,事情也会改变。 Trying to build formatting rules into your application immediately introduces a maintenance item.
尝试在应用程序中建立格式设置规则会立即引入一个维护项目。 When (when) the US runs into the limits of its current (xxx) yyy-zzzz format, for example, a huge number of applications are going to need updates.
例如,当(当)美国遇到其当前(xxx)yyy-zzzz格式的限制时, 大量的应用程序将需要更新。 Painful ones.
痛苦的。 Similarly, I used an application the other day that assumed all UK numbers were in the form (xxxxx) nnn nnn.
同样,前几天我使用了一个应用程序,该应用程序假设所有英国号码都采用(xxxxx)nnn nnn的形式。 This is no longer true, large metropolitan areas are now (xxx) nnnn nnnn whereas we more rural types are still on the old system.
这不再是事实,大都市区现在是(xxx)nnnn nnnn,而我们更多的农村类型仍在旧系统上。 For a while at the beginning of the last decade, London's numbers were in the form (xxxx) nnn nnnn.
在过去十年的初期,伦敦的数字以(xxxx)nnn nnnn的形式出现。 You get the idea.
你明白了。
The code you've shown us could not possibly have given the compile error mentioned in your subject. 您显示给我们的代码不可能给出您主题中提到的编译错误。 The real problem is that the integer literal starts with a 0, making it an octal (base 8) constant.
真正的问题是整数文字以0开头,使其成为八进制(以8为底)常量。 Some of the digits (8 and 9s) aren't valid in octal literals.
某些数字(8和9)在八进制文字中无效。
As several other folks have already pointed out, phone "numbers" should be represented as String
, not long
or any other numeric type, so that you can avoid this problem and many others. 正如其他一些人已经指出的那样,电话“数字”应表示为
String
,而不是long
或任何其他数字类型,以便可以避免出现此问题以及其他许多问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.