NSNumber 存储浮点数。 请问没有科学记数法吗?

发布于 2024-07-11 09:56:01 字数 237 浏览 10 评论 0原文

我的代码看起来像这样,

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

文本字段中的值实际上是一个电话号码。 它以烦人的 (2.0)78966e+08 的形式存储在 NSNumber 中,

我怎样才能让 NSNumber 将其存储为 0207896608?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

一身软味 2024-07-18 09:56:01

我认为将电话号码存储到 NSNumber 中的基本思想是有缺陷的:

  • 如何区分带或不带前导 0 的数字?
  • 如何存储国外的电话号码?

我会使用 NSString 代替 NSNumber。

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.

我们的影子 2024-07-18 09:56:01

仅仅因为它被称为数字并不意味着“电话号码”是与“5”或“pi”相同意义上的数字。

您应该将电话号码视为字符串,或者您应该创建一个 TelephoneNumber 模型类来表示每个电话号码。

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.

七婞 2024-07-18 09:56:01

考虑一下世界上有些地方的数字没有前导 0,并且有前导 0 的数字与没有前导 0 的相同数字不同。

05843924 != 5843924

所以别再偷懒了 ​​NSNumber 破解并构建您自己的电话号码类。

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.

甜味超标? 2024-07-18 09:56:01

科学记数法在许多计算机语言中用作非常大(或非常小)数字的默认输出。 如果您希望数字以小数形式输出,则需要指定输出格式(实现因语言而异)。

此外,julesjacobs 是正确的。 您不应将 FLOAT 用于电话号码,因为它可能会出现二进制舍入错误。 使用 INT 或 STRING 将为您省去很多麻烦。

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.

赠意 2024-07-18 09:56:01

如果您需要能够将其作为数字处理,也许您应该将其分解为多个部分,并将每个部分存储为整数。

01112223333
country code 0
area code 111
prefix 222
number 3333

或者,如果不需要操作它,您可以将整个内容存储为字符串。

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.

静若繁花 2024-07-18 09:56:01

您是否将电话号码存储在浮点数中? 您应该考虑使用整数或字符串。 也许:

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

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]];
原谅我要高飞 2024-07-18 09:56:01

嘿伙计们你觉得怎么样,这似乎满足了我的目的。 目前只有英国,所以当我有机会时我会担心本地化。

我用它来存储号码

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

,这个方法格式化我的电话号码并处理前面提到的 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; 
    }
}

感谢所有的评论。 我将把答案标记为建议 NSString 的人,因为我担心我会恢复使用 NSString 而不是我上面的解决方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文