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

- 9,206
- 15
- 49
- 74

- 3,773
- 12
- 47
- 79
-
Answer updated, wanted to write `string` only. – Mohit Bhansali Jun 30 '13 at 03:20
-
2An `Int32` (`int`) handles up to ~2 1/4 billion. Telephone numbers (with area codes) can be up to 9,999,999,999 (10 billion if you include (000)-000-0000). Also, this failure is already _on_ [The Daily WTF](http://thedailywtf.com/Articles/Confessions-The-Phone-Number.aspx). I expect this question _and_ this comment get _very_ popular so everyone can see this. – Cole Tobin Jun 30 '13 at 03:26
-
@ColeJohnson - I was just going to comment about that - I recall seeing a story (I think) on the Daily WTF about a situation where the phone number was being stored as Int32, and the max value (2147483647) actually corresponded to a live phone number (or something along those lines). – Tim Jun 30 '13 at 03:27
-
@Tim that's actually what I was looking for after commenting! :D Updated with link (Google `phone number site:thedailywtf.com`). Also, that phone number is still active I believe. Poor souls. – Cole Tobin Jun 30 '13 at 03:28
-
@ColeJohnson - Thanks for finding (and posting) the link. :) – Tim Jun 30 '13 at 03:29
-
Closing a C# question as a duplicate of a Java question. Hmmm.... – HappyNomad Mar 16 '15 at 00:30
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.

- 2,193
- 18
- 21
-
4
-
This may sound dumb but isn't the phone number a number? There is uint64 so I actually see no reason to use a string unless you don't want to validate it(which can only be a bad practice). – themihai May 02 '15 at 20:04
-
8In fact, it's more of a code represented by a sequence of digits. But it's surely not treated as a number. It doesn't make sense to do arithmetic operations on a phone number. What about a number starting with zeros? In France, phone numbers are in forms of 0x xx xx xx xx, using uint64 would mean you lose the leading zero. – Phil-R May 03 '15 at 22:41
I recommend using a string since that gives you more flexibility when it comes to formatting and non numeric characters like extension etc.

- 38,769
- 12
- 102
- 135
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.

- 1
- 1

- 1,725
- 4
- 20
- 43
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.

- 1,571
- 4
- 22
- 32