views:

2604

answers:

8

(iPhone question) My code looks like this

NSNumber *inputToNumber = [NSNumber numberWithFloat:[textField.text floatValue]];

the value from the textfield is actually a telephone number. It's stored in NSNumber as an annoying (2.0)78966e+08

How can I just get NSNumber to store it as 0207896608?

Thanks

dan

A: 

Ah OK I got it:

NSNumber *inputToNumber = [NSNumber numberWithInt:(int)[textField.text floatValue]];

Dan Morgan
Please don't put this sort of thing in answers, use comments like this instead.
Darron
Why are you casting the textField.text to an int? Can't you just use intValue instead of floatValue?
Sean
Chris Hanson
Comments require 50 karma, so at the moment, he *can't* post a comment.
Peter Hosey
He can edit the question.
Dour High Arch
Sure, but Darron's suggestion was to “use comments”, which he cannot do.
Peter Hosey
A: 

Note that I don't any any experience with objective-c, but are you seriously storing a phone number in a float? You should consider using an integer or string. Perhaps:

NSNumber *inputToNumber = [NSNumber numberWithInt:[textField.text intValue]];
Jules
And how do you represent ‘*’, ‘#’, or ‘,’ in that integer?
Peter Hosey
+1  A: 

Scientific notation is used in may computer languages as the default output of very large (or very small) numbers. If you want the number to be output as a decimal, you need to specify the output format (the implementation varies by language.)

Also, julesjacobs is correct. You should not use FLOAT for a phone number as it is subject to binary rounding errors. Using INT or STRING will save you lots of headaches.

Chris Nava
No, you can’t use an int. Phone numbers may begin with zero, and can contain semantically meaningful non-digits such as # or *.Unless you’re really, really sure you know what’s a valid phone number, don’t go making assumptions about it. (This goes for many types of input validation.)
Ahruman
I agree. I was referring to US telephone numbers as that's all I ever have to deal with. For international numbers and INT would not do.
Chris Nava
+30  A: 

I think that the basic idea to store a phone number into a NSNumber is flawed:

  • how do you discriminate between numbers with or without leading 0 ?
  • how do you store phone numbers from foreign countries ?

I would use NSString in place of NSNumber.

mouviciel
A: 

If you need to be able to deal with it as numbers maybe you should break it up into its parts, and store each part as an integer.

01112223333
country code 0
area code 111
prefix 222
number 3333

Or you could store the whole thing as a string if you don't need to manipulate it.

Ryan Townshend
Phone numbers aren't in the same format in all countries. A single phone number may even look different in country B from what it looks like in country A. And even if you can figure out which chunks are which, what of ‘*’, ‘#’, and ‘,’? Integers still won't work.
Peter Hosey
A: 

Hey Guys what do you think of this, It seems to full-fill my purposes. Only UK at the moment so will worry about localization when I get a chance.

I use this to get to store the number

NSNumber *inputToNumber = [NSNumber numberWithLongLong:(long long)[[textField.text stringByReplacingOccurrencesOfString:@" " withString:@""] longLongValue]];

And this method formats my telephone number and takes care of the preceeding 0 mentioned.

-(NSString *)phoneNumberString:(NSNumber *)phoneNumber {
    //Add a zero because NSNumber won't save a preceeding zero
    NSString *telephoneString = [[NSString alloc] initWithFormat:@"0%@", [phoneNumber stringValue]];

    if (telephoneString.length >= 4) {
        NSString *firstPart = [[NSString alloc] initWithString: [telephoneString substringToIndex:4]];
        NSString *secondPart = [[NSString alloc] initWithString: [telephoneString substringFromIndex:4]];

        //Add the two parts together with a space inbetween
        NSString *formattedTelephoneString = [NSString stringWithFormat:@"%@ %@", firstPart, secondPart];

        //send it back to the cellForRow TableCell Method
        [firstPart release];
        [secondPart release];
        [telephoneString release];

        return formattedTelephoneString;    
    }
    else {
        return telephoneString; 
    }
}

Thanks for all the comments. I'm gonna mark the answer as whoever suggested NSString as I fear I will revert to using NSString for this instead of my above workaround.

Dan Morgan
your code is leaking memory. you should auto release telephoneString before returning it, or use the convenience method stringWithString instead of [[NSString alloc ] initWith..], that way you don't need to worry about releasing.
lajos
What I meant about the preceeding 0 is that not all phone numbers have a preceeding 0 and with an integer value you have no way to code this (except if you use the minus sign for that purpose).
mouviciel
+8  A: 

Just because it's called a number doesn't mean a "telephone number" is a number in the same sense that "5" or "pi" are.

Either you should treat a telephone number as a string, or you should create a TelephoneNumber model class to represent each one.

Chris Hanson
+5  A: 

guys, consider that there are places in the world where numbers don't have leading 0's and where a number with a leading 0 is not the same as the same number without a leading 0.

05843924 != 5843924

so stop being lazy with that NSNumber hacks and build your own phone-number class.