Why is it best to store a telephone number as a string vs. integer?

前端 未结 5 553
死守一世寂寞
死守一世寂寞 2021-02-01 12:50

As the question states, why is it considered best practice to store telephone numbers as strings rather than integers in the telephone_number column?

Not sure I understa

相关标签:
5条回答
  • 2021-02-01 13:09

    Telephone numbers are strings of digit characters, they are not integers.

    Consider for example:

    • Expressing a telephone number in a different base would render it meaningless

    • Adding or multiplying two telephone numbers together, or any math operation on a phone number, is meaningless. The result is not another telephone number (except by conicidence)

    • Telephone numbers are intended to be entered "as-is" into a connected device.

    • Telephone numbers may have leading zeroes.

    • Manipulations of telephone numbers, such as adding an area code, are String operations.

    Storing the string version of the telephone number makes this clear and unambiguous.


    History: On old pulse-encoded dial systems, the code for each digit in a telephone number was sent as the same number of pulses as the digit (or 10 pulses for "0"). That may be why we still use digits to represent the parts of a phone number. See http://en.wikipedia.org/wiki/Pulse_dialing

    0 讨论(0)
  • 2021-02-01 13:12

    What Neil Slater said is correct. I would add that there are lots of edge cases where you can't express a telephone number as a number value consistently.

    For example, consider these numbers:

    011-123-555-1212
    +11-123-555-1212
    +1 (112) 355-5121 x2
    

    These are all potentially valid phone numbers, but they mean very different things. Yet, in integer form, they are all 111235551212.

    0 讨论(0)
  • 2021-02-01 13:19

    If you are going to store the number for display from input, then you must use a string.

    However, while it is true that no mathematical operations can be performed on a number that have meaning. Using a number in hashsets and for indexing is quicker than using a string. So provided you can guarantee or homogenise your set of numbers, so they are all consistent, then you may see better performance operating on a number.

    For example, in the Telco world, rating calls for a given customer includes a lot of searching on their CLI and in this situation it is faster and cheaper to search by integer. Generally though strings will be fine performance wise, it is only where performance matters and you have multiple searches to perform for a huge range of numbers - i.e. Rating 250 million calls across 2 million lines and 2000 tariffs. In memory rating also gets expensive, so being able to use a 64bit int or uint is cheaper when dealing with these volumes.

    0 讨论(0)
  • 2021-02-01 13:23

    Consider these phone numbers for example

    099-1234-56789 or +91-8907-687665.

    In this case,if the phone_number attribute is of type integer,then it can't accept these values.It should be a string to hold these type of values.So string is always preferred than integer

    0 讨论(0)
  • 2021-02-01 13:23

    There is several reasons for this :

    • Phone numbers often start with a "0" : an integer will remove all leading "0"s
    • Phone number can have special char : +, (, -, etc. (for exemple : +33 (0)6 12 23 34)
    • You cannot perform operations on phones : adding phones, for instance, would be meaningless
    • Phone number may be internationalised, i.e. different format for different people, thus not possible with integers

    There might be other reasons, but I guess that's already a fair amount of those :)

    0 讨论(0)
提交回复
热议问题