如何将 NSString 转换为 NSNumber
如何转换包含许多任何原始数据类型的 NSString
(例如 int
、float
、char
、 unsigned int
等)?问题是,我不知道字符串在运行时将包含哪种数字类型。
我知道如何做到这一点,但我不确定这是否适用于任何类型,也适用于无符号和浮点值:
long long scannedNumber;
NSScanner *scanner = [NSScanner scannerWithString:aString];
[scanner scanLongLong:&scannedNumber];
NSNumber *number = [NSNumber numberWithLongLong: scannedNumber];
感谢您的帮助。
How can I convert a NSString
containing a number of any primitive data type (e.g. int
, float
, char
, unsigned int
, etc.)? The problem is, I don't know which number type the string will contain at runtime.
I have an idea how to do it, but I'm not sure if this works with any type, also unsigned and floating point values:
long long scannedNumber;
NSScanner *scanner = [NSScanner scannerWithString:aString];
[scanner scanLongLong:&scannedNumber];
NSNumber *number = [NSNumber numberWithLongLong: scannedNumber];
Thanks for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
使用
NSNumberFormatter
:如果字符串不是有效数字,则
myNumber
将为nil
。如果它是一个有效的数字,那么您现在就可以利用NSNumber
的优点来确定它实际上是什么类型的数字。Use an
NSNumberFormatter
:If the string is not a valid number, then
myNumber
will benil
. If it is a valid number, then you now have all of theNSNumber
goodness to figure out what kind of number it actually is.您可以使用
-[NSString integerValue]
、-[NSString floatValue]
等。但是,正确的(区域设置敏感等)方法是使用-[NSNumberFormatter numberFromString:]
它将为您提供一个从适当的语言环境转换而来的 NSNumber,并给出NSNumberFormatter
的设置(包括是否允许浮点值)。You can use
-[NSString integerValue]
,-[NSString floatValue]
, etc. However, the correct (locale-sensitive, etc.) way to do this is to use-[NSNumberFormatter numberFromString:]
which will give you an NSNumber converted from the appropriate locale and given the settings of theNSNumberFormatter
(including whether it will allow floating point values).Objective-C
(注意:此方法在不同的语言环境中表现不佳,但比
NSNumberFormatter
稍快)Swift
简单但肮脏的方式
扩展 -方式
这实际上更好,因为它可以很好地处理区域设置和小数。
现在您可以简单地执行以下操作:
Objective-C
(Note: this method doesn't play nice with difference locales, but is slightly faster than a
NSNumberFormatter
)Swift
Simple but dirty way
The extension-way
This is better, really, because it'll play nicely with locales and decimals.
Now you can simply do:
对于以整数开头的字符串,例如
@"123"
、@"456 ft"
、@"7.89"
等,请使用 < a href="https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/integerValue" rel= “noreferrer”>-[NSString integerValue]
。因此,
@([@"12.8 lbs" integerValue])
就像做[NSNumber numberWithInteger:12]
一样。For strings starting with integers, e.g.,
@"123"
,@"456 ft"
,@"7.89"
, etc., use-[NSString integerValue]
.So,
@([@"12.8 lbs" integerValue])
is like doing[NSNumber numberWithInteger:12]
.你还可以这样做:
玩得开心!
You can also do this:
Have fun!
如果您知道收到整数,则可以使用:
If you know that you receive integers, you could use:
这是 NSNumberFormatter 读取本地化数字 NSString (xCode 3.2.4, osX 10.6) 的工作示例,以节省其他人我刚刚花费的时间。请注意:虽然它可以处理尾随空格(“8,765.4”有效),但它不能处理前导空格,也不能处理杂散文本字符。 (错误的输入字符串:“8”和“8q”和“8 q”。)
Here's a working sample of NSNumberFormatter reading localized number NSString (xCode 3.2.4, osX 10.6), to save others the hours I've just spent messing around. Beware: while it can handle trailing blanks ("8,765.4 " works), this cannot handle leading white space and this cannot handle stray text characters. (Bad input strings: " 8" and "8q" and "8 q".)
我想将字符串转换为双精度数。上面的答案对我来说不太有效。但这做到了:How to do string conversions in Objective-C?
我几乎所做的就是:
I wanted to convert a string to a double. This above answer didn't quite work for me. But this did: How to do string conversions in Objective-C?
All I pretty much did was:
谢谢大家!我结合反馈,最终设法从文本输入(字符串)转换为整数。另外它可以告诉我输入是否是整数:)
Thanks All! I am combined feedback and finally manage to convert from text input ( string ) to Integer. Plus it could tell me whether the input is integer :)
我认为 NSDecimalNumber 会做到这一点:
示例:
NSDecimalNumber 是 NSNumber 的子类,因此允许隐式转换。
I think NSDecimalNumber will do it:
Example:
NSDecimalNumber is a subclass of NSNumber, so implicit casting allowed.
C 的标准
atoi
怎么样?您认为有什么注意事项吗?
What about C's standard
atoi
?Do you think there are any caveats?
您可以只使用
[string intValue]
或[string floatValue]
或[string doubleValue]
等您也可以使用
NSNumberFormatter
类:You can just use
[string intValue]
or[string floatValue]
or[string doubleValue]
etcYou can also use
NSNumberFormatter
class:你也可以这样做代码8.3.3 ios 10.3支持
you can also do like this code 8.3.3 ios 10.3 support
试试这个
注意 - 我已经根据我的要求使用了 longLongValue 。您还可以根据您的要求使用integerValue、longValue 或任何其他格式。
Try this
Note - I have used longLongValue as per my requirement. You can also use integerValue, longValue, or any other format depending upon your requirement.
工作于Swift 3
Worked in Swift 3
我知道这已经很晚了,但下面的代码对我有用。
尝试此代码
NSNumber *number = @([dictionary[@"keyValue"] intValue]]);
这可能会对您有所帮助。谢谢
I know this is very late but below code is working for me.
Try this code
NSNumber *number = @([dictionary[@"keyValue"] intValue]]);
This may help you. Thanks