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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:13:52  来源:igfitidea点击:

How to convert a string to float?

objective-cioscocoa-touchnsstringfloating-point

提问by Matoe

Possible Duplicate:
Convert string to float in Objective-C

可能的重复:
在 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 floatValueto the NSStringobject. NSString::floatValue

只需将消息传递floatValueNSString对象。NSString::floatValue