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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

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.

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.

Are you storing a phone number in a float? You should consider using an integer or string. Perhaps:

NSNumber *inputToNumber = [NSNumber numberWithInt:[textField.text intValue]];

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top