Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the right way to represent phone numbers?

I'm having trouble representing a mobile number in one of my applications.

I was wondering if there is an Integer class that will allow you to store such a number starting with 0417254482. Perhaps using a string be a more appropriate? At present when I'm trying to use represent a phone number with ints, doubles longs I seem to store random numbers and not the numbers I meant to store.

like image 714
user420344 Avatar asked Aug 14 '10 11:08

user420344


People also ask

Should you put dashes in phone number?

Hyphens are also used to separate numbers that are not inclusive, such as telephone numbers or Social Security numbers. Hyphens are likewise used in URLs and email addresses, and to spell out a word letter by each letter.

Why are there parentheses in phone numbers?

Not only telephone users but also those who write telephone numbers will be "called on" to make the changes. A small adjustment had to be made, since the traditional parentheses around area codes indicate that dialling the code is not necessary for all calls.

What is a valid US phone number format?

Phone numbers in the United States typically consist of 11 digits — the 1-digit country code, a 3-digit area code and a 7-digit telephone number. The 7-digit telephone number is further comprised of a 3-digit central office or exchange code and a 4-digit subscriber number. The country code of USA is +1.

Why do people put a 1 before their phone number?

It used to be possible to call someone within your own area code without having to add that area code; later, in many places, you had to add the area code. At some point it became required in many areas to dial the “1” before the 10 digits, even if you were calling just across the street.


2 Answers

Use String. Aside from anything else, you won't be able to store leading zeroes if you use integers. You definitely shouldn't use int (too small) float or double (too much risk of data loss - see below); 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.

In terms of the "data loss" mentioned above for float and double - float definitely doesn't have enough precision; double could work if you're happy that you'll never need more than 16 digits (a couple fewer than you get with long) but you would need to be very, very careful that anywhere you convert the value back from double to string, you got the exact value. Many formatting conversions will give you an approximation which may be accurate to, say, 10 significant digits - but you'd want an exact integer. Basically, using floating point for phone numbers is a fundamentally bad idea. If you have to use a fixed-width numeric type, use a long, but ideally, avoid it entirely.

like image 109
Jon Skeet Avatar answered Sep 22 '22 10:09

Jon Skeet


Think about this: Is a phone number really a number? Does it make sense adding (or make another arithmetic operation) with phone numbers? Phone numbers are codes, they're usually represented with numbers, but that's just a convention and, maybe, in another country the use letters too (I've just realized, what about international phone numbers? they have a + at the beginning. You have to think about the nature of the things you want to represent, and then, find the most suitable representation.

like image 26
manolowar Avatar answered Sep 22 '22 10:09

manolowar