NSString的精确浮点值 [英] Exact float value from a NSString

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

问题描述

  NSString * str = @37.3336; 
float f = [str floatValue];

f是37.3335991。



自己舍去这个,有没有办法从NSString中获得确切的浮点值?

解决方案使用 [NSDecimalNumber decimalNumberWithString:locale:] 。这将以十进制格式而不是二进制格式保存数字。


NSString *str = @"37.3336";
float f = [str floatValue];

f is 37.3335991.

Aside from rounding this myself, is there a way to get the exact float value from a NSString?

解决方案

Use [NSDecimalNumber decimalNumberWithString:locale:]. This will maintain the number in a decimal format rather than a binary format.

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

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