12
我想一个字符串转换为浮动。
/* dict is an NSDictionary to load Preferences */
NSString *str = [dict objectForKey:@"key"];
这是我得到的地方。现在我想将浮点数中的字符串值(本例中为@"32.0f"
)转换为可由我的应用程序处理的值。我怎样才能做到这一点?
我想一个字符串转换为浮动。
/* dict is an NSDictionary to load Preferences */
NSString *str = [dict objectForKey:@"key"];
这是我得到的地方。现在我想将浮点数中的字符串值(本例中为@"32.0f"
)转换为可由我的应用程序处理的值。我怎样才能做到这一点?
CGFloat strFloat = (CGFloat)[str floatValue];
消息floatValue
只是传递给NSString
对象。 NSString::floatValue
这不是一个浮点数。浮点原语有一个SINGLE小数点,就像算术中的十进制数字一样。 – Sid 2014-05-29 14:03:52
**对于前面的例子,SORRY ** _consider this one_ :: 它在点之后跳过数字,例如:'NSString * str = @“1.12”; CGFloat strFloat =(CGFloat)[str floatValue];'_ // 1.0000_ **请按照这一个** http://stackoverflow.com/questions/8011706/objective-c-convert-long-and-浮动到字符串/ 23932420#23932420 – asmad 2014-05-30 11:48:14
Asmad,我把你的代码粘贴到Xcode中,运行它,然后放置一个断点,它可以正常工作 - http://i.imgur.com/kUXigNn.png – Sid 2014-06-08 03:52:06