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

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


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


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.


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