简体   繁体   English

长度不应该足够大以能够存储电话号码吗?

[英]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.

相关问题 java Date(long)没有道理。long不足以存储当前日期(以毫秒为单位) - java Date(long) doesn't make sense.. a long isn't big enough to store the current date in milliseconds java是否足够长以微秒为单位存储纪元时间? - Is a java long enough to store epoch time in microseconds? 我是否需要添加大量的“检查”以防止用户修改他们在RESTful应用程序中不应该能够访问的数据? - Do I need to add a large number of “checks” in order to prevent users from modifying data they shouldn't be able to in a RESTful application? 超类引用不应该能够调用其子类专有的方法,但是 - A superclass reference shouldn't be able to call methods exclusive to it's subclass but 我不应该能够从子类访问方法吗? - Shouldn't I be able to access a method from sub class? 如果Controller有一个@Controller注释,那么对于没有组件扫描的Spring来说,这应该不够吗? - If a Controller has a @Controller annotation, shouldn't that be enough for Spring without component scanning? 什么时候BIG,足够大的数据库? - When is BIG, big enough for a database? 如何将长整数分解为手机号码? - How to break a long integer into mobile phone number? 如何验证8到10位数字的长电话号码? - How to validate 8 to 10 digit long phone number? 有什么理由我不应该将我的密钥库存储在版本控制中吗? - Is there any reason I shouldn't store my keystore in version control?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM