When setting a mobile phone number variable using long or float it says ‘Error: integer number to large: 07859664443’
Here’s the code
public class StudentNode
{
public long TelNumber;
public void setTelNumber(long aTelNumber)
{
TelNumber = aTelNumber;
}
}
Shouldn’t a long be big enough to be able to store a phone number?
Telephone numbers aren’t numbers. They’re strings. For instance, in the UK and many other European countries, the first digit of a full phone number is a 0. But a simple numeric type like
longhas no way to indicate that a leading 0 is significant, the number 07859 664 443 would be the value7859664443. The leading zero matters. Separately, it’s not at all uncommon to see a number written like this: +44 (0)7859 664 443. Put that in yourlongand try to smoke it. 🙂Also, the formatting matters. 07859 664 443 is a lot easier to read and dial than 07859664443. In the U.S., they frequently put area codes in parentheses: (800) 123-4567, which again is easier for we poor humans to deal with than 8001234567.
In my experience, the best way to deal with phone numbers is to store them as strings, and largely to leave them the way they were entered, unless you want to limit the application to the phone numbers used in a very limited geographic area — and even then, things can change. Trying to build formatting rules into your application immediately introduces a maintenance item. When (when) the U.S. runs into the limits of its current (xxx) yyy-zzzz format, for example, a huge number of applications are going to need updates. Painful ones. Similarly, I used an application the other day that assumed all UK numbers were in the form (xxxxx) nnn nnn. This is no longer true, large metropolitan areas are now (xxx) nnnn nnnn whereas we more rural types are still on the old system. For a while at the beginning of the last decade, London’s numbers were in the form (xxxx) nnn nnnn. You get the idea.