ios 如何将字符串转换为浮点数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6240601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert a string to float?
提问by Matoe
Possible Duplicate:
Convert string to float in Objective-C
I'd like to convert a string to a float.
我想将字符串转换为浮点数。
/* dict is an NSDictionary to load Preferences */
NSString *str = [dict objectForKey:@"key"];
This is where I got. Now I'd like to convert the string value (in this case @"32.0f"
) in a float, where it could be processed by my application. How can I do this?
这是我得到的地方。现在我想将字符串值(在本例中@"32.0f"
)转换为浮点数,我的应用程序可以在其中处理它。我怎样才能做到这一点?
回答by Sid
CGFloat strFloat = (CGFloat)[str floatValue];
回答by Mahesh
Just pass the message floatValue
to the NSString
object. NSString::floatValue
只需将消息传递floatValue
给NSString
对象。NSString::floatValue