The NSNumber of the float storage. Please, not scientific notation?

My code looks like this

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

      

the value from the textbox is actually the phone number. It is stored in NSNumber as annoying (2.0) 78966e + 08

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

+1


source to share


7 replies


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



Also, julesjacobs is correct. You shouldn't use FLOAT for a phone number as it is prone to rounding errors. Using INT or STRING will save you a lot of headaches.

+1


source


I think the basic idea of ​​storing the phone number in NSNumber is wrong:

  • how do you distinguish numbers with or without leading 0?
  • How do you store phone numbers from abroad?


I would use NSString instead of NSNumber.

+34


source


Just because it is called a number does not mean that "phone number" is a number in the same sense as "5" or "pi".

Either you have to treat the phone number as a string, or create a TelephoneNumber model class to represent each one.

+9


source


Note that there are places in the world where numbers do not have a leading 0, and a number with a leading 0 does not match the same number without a leading 0.

05843924 != 5843924

      

So stop NSNumber

getting lazy with these hacks and create your own phone number.

+5


source


If you need to deal with it like numbers, perhaps you should break it down into parts and save each part as a whole.

01112223333
country code 0
area code 111
prefix 222
number 3333

      

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

-1


source


Do you store the phone number in the float? You must use an integer or string. Maybe:

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

      

-3


source


Hey guys, what do you think about this? This seems to fulfill my goals. Only the UK at the moment will be so concerned about localization when I get the chance.

I use this to get the number

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

      

And this method formats my phone number and takes care of the previous 0.

-(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 am going to mark the answer as the one who suggested NSString as I am afraid I will revert to using NSString for this instead of my above solution.

-five


source







All Articles