phone number should be a string or some numeric ty

2020-03-01 08:46发布

问题:

We want to store 10 digit mobile number i.e.9999999999. Should it be numeric or string datatype? We don't want to do any calculative or manipulation operation on this

Which is better memory and performance wise?

回答1:

ITU-T recommendation E.164 says you need 3 digits for the country code and up to 15 digits for the directory number within the country dialing plan.

And, many people add some punctuation. For example:

+1.212.555.1212 is a North American number. It could also be rendered (212) 555-1212 in a North American centric app.

32 characters of text should do the trick worldwide.

DO NOT use a number, or you'll be sorry. I was: two things.

  • Lost some European business for a company because we assumed all phone numbers were NANP-compliant ten-digit numbers.
  • A spreadsheet export rendered the numbers in scientific notation 2.12555E+09 That's almost as stupid as SIRI telling me you have call from two bllion, one hundred twenty five million....

Telephone directory numbers are not numeric data types. Take a look at this: Falsehoods Programmers Believe About Telephone Numbers.



回答2:

It must be a stringas phone number will exceed the limit of int or even long. So for handling those scenario string is always prefered.



回答3:

A couple of things that are good to keep in mind:

In general, a number that you're not planing to do calculations on, should be stored as a form of string. If you need the number to be able to start with a zero (as you do, because some phone numbers do), this is essential.

Ergo, even if (or when) you're able to store a phone number as a huge integer (you're able to with 64 bit integers), you shouldn't. You will loose data as soon as a number starts with a zero.

When storing a phone number, remember that the country code is a special type of information. You would do best if you separate this from the rest of the number when you store it. This way you can easily query phone numbers by country, and you won't have to deal with parsing of the number more than once (before you store it, rather than every time you fetch it). Also, if you store a phone number together with the country code, you'll need to validate the thing to ensure you always store the country code, because two numbers from two different countries could potentially be identical if one has a country code and the other one doesn't.

Also remember that punctuation is a method of presentation, and thus has nothing to do with the way you store the data. You can always choose to present data the way you want, and the way you want will in all cases depend on a number of factors. For example what kind of data you're presenting, to whom you're presenting it, and in some cases even when you're presenting the data. In the case of phone numbers, you should store the number as a string, without any of the punctuation.

I would recommend that you have a look at Google's library for parsing, formatting and validating international phone numbers (https://github.com/googlei18n/libphonenumber). You can feed this library your phone number and country code, and it will give you a whole lot of useful information about it, such as whether the number is possible and valid, which region it belongs to, what kind of number it is, etc.

Pro tip I: Provide your users with a way to select which country the number belongs to, rather than having them type the country code. Better for you, and better for your users.

Pro tip II: There's rarely such a thing as a "North American centric app" or "insert region here centric app", especially if your app is available on the web. It does happen, but it is rare, so you would be wise to prepare your app for the world, rather than a smaller part of it.



回答4:

you can use "ulong" for storing long integer values. and to read the particular value or phone number, you can use "Convert.ToUInt64(Console.ReadLine());"