Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I store telephone numbers as strings or integers? [duplicate]

Tags:

c#

I'm trying to decide between storing a phone number as a string or an int. Any ideas?

like image 265
Flood Gravemind Avatar asked Jun 30 '13 03:06

Flood Gravemind


People also ask

Should phone numbers be stored as string?

A phone number should always be stored as a string or text and never an integer. Some phone numbers generally use hyphens and possibly parentheses. Also, you might need to indicate the country code before the phone number such as +46 5555-555555.

Which data type should be used to store a telephone number?

String (str or text) A phone number is usually stored as a string (+1-999-666-3333) but can also be stored as an integer (9996663333).

How should you store a phone number in a database?

Therefore, it is highly recommended that the phone numbers are stored along with their county code to avoid data ambiguity. This will prevent data duplication and standardise your formatting. E. 164 is an international format which defines a general format for international telephone numbers.

Why is it a good idea to use a string variable to store a phone number than using an integer?

Phone number may be internationalised, i.e. different format for different people, thus not possible with integers.


4 Answers

For any situation like these, think of : will I have to calculate anything with that value? If that doesn't make any sense, you should use a string. In that case, there's no logical case where you'd use the telephone number as a number, so use a string.

like image 52
Phil-R Avatar answered Sep 29 '22 12:09

Phil-R


I recommend using a string since that gives you more flexibility when it comes to formatting and non numeric characters like extension etc.

like image 45
TGH Avatar answered Oct 01 '22 12:10

TGH


I would suggest using String - aside from anything else, otherwise you won't be able to store leading zeroes. You definitely shouldn't use int (too small) float or double (too much risk of data loss); long or BigInteger could be appropriate (aside from the leading zeroes problem), but frankly I'd go with String. That way you can also store whatever dashes or spaces the user has entered to make it easier to remember the number, if you want to.

Reference: What's the right way to represent phone numbers?

like image 32
Mohit Bhansali Avatar answered Oct 01 '22 12:10

Mohit Bhansali


I highly recommend you use a string for this.

If you are going to validate phone number input then you can use the regex lib's matcher and pattern to make sure a phone number was entered in the correct format.

like image 21
Tdorno Avatar answered Oct 01 '22 12:10

Tdorno