将NSString值转换为浮点值时得到0.0000 [英] getting 0.0000 when converting NSString value to float

查看:176
本文介绍了将NSString值转换为浮点值时得到0.0000的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我试图将NSString值转换为浮点使用



$ p $ N $> student_Percentage];
float f = [value floatValue];

但是我得到f的值为0.00000。我尝试使用NSNumberFormatter,NSNumber ...但仍然得到0.00000。

解决方案


如果接收方不是以有效的
文本开头的,floatValue返回0.0表示一个浮点数。


值应该是8.9 。
从字符串中删除双引号。

  NSMutableString * value = [dict objectForKey:@student_Percentage ]; 
[value replaceOccurrencesOfString:@\withString:@options:NSCaseInsensitiveSearch range:NSMakeRange(0,[value length])];
float f = [value floatValue];


In my application, i tried to convert NSString value to float using

NSString *value = [dict objectForKey:@"student_Percentage"];
float f = [value floatValue];

But I'm getting the value of f as 0.00000. I tried with NSNumberFormatter, NSNumber... but still get 0.00000.

解决方案

floatValue returns 0.0 if the receiver doesn’t begin with a valid text representation of a floating-point number.

[dict objectForKey:@"student_Percentage"] value should be like 8.9.
Remove double quotes from your string.

NSMutableString *value = [dict objectForKey:@"student_Percentage"];
[value replaceOccurrencesOfString:@"\"" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [value length])];
float f = [value floatValue];

这篇关于将NSString值转换为浮点值时得到0.0000的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆