@"0.01" 转换成float时, 经常会变成  0.009999799 这种形式, 因为float类型无法精准保存, 系统会选一个接近的值来代替.

而double类型则可以有更好的精度.

http://stackoverflow.com/questions/9328260/converting-nsstring-to-float-adds-in-decimal-places

Your issue seems to be that you don't understand how floats are stored in memory and don't know that floats aren't precise.

Exact values often can't be stored and so the system picks the closest number it can to represent it. If you look carefully, you can see that each of the outputted numbers is very close to your inputted values.

For better accuracy, try using double instead. Double does encounter the same problems, but with better precision. Floats have about 6 significant digits; doubles have more than twice that.Source

Here are some other StackOverflow answers and external articles you should read:

NSString 转换 float 的精度问题, 换double类型可以解决的更多相关文章

  1. 浮点型 float和double类型的内存结构和精度问题

    首先引用一个例子在java中可能你会遇到这样的问题: 例:0.99999999f==1f //true 0.9999999f==1f //false 这是超出精度造成的,为了知道为什么会造成这样的问题 ...

  2. c# float类型和double类型相乘出现精度丢失

    c# float类型和double类型相乘出现精度丢失 double db = 4.0; double db2 = 1.3; float f = 1.3F; float f2 = 4.0F; Deci ...

  3. C# double类型精度丢失问题

    我们先看一段代码,可以在控制台程序中执行看看结果 { double d = 500; double d1 = 233.84; double d2 = d - d1; //d2=266.15999999 ...

  4. Effective Java 第三版——60. 需要精确的结果时避免使用float和double类型

    Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...

  5. [ JAVA编程 ] double类型计算精度丢失问题及解决方法

    前言 如果你在测试金融相关产品,请务必覆盖交易金额为小数的场景.特别是使用Java语言的初级开发. Java基本实例 先来看Java中double类型数值加.减.乘.除计算式实例: public cl ...

  6. float和double类型的存储方式

    Float double 类型在计算机的存储方式 计算机中只认识10的二进制数,那么该如何存储小数呢? 那么我们先看Floa类型: Float在计算机(32位)中是4个字节的,具体地:第一位为符号位0 ...

  7. Python之☞float浮点数精度问题

    Python的浮点数损失精度问题(转) 一个简单的面试题: >>>0.1+0.1+0.1 0.2 >>>0.1+0.1+0.1 0.3000000000000000 ...

  8. impala 四舍五入后转换成string后又变成一个double的数值解决(除不尽的情况)

    impala 四舍五入后转换成string后又变成一个double的数值解决(除不尽的情况)例如Query: select cast(round(2 / 3, 4)*100 as string)+-- ...

  9. C语言中float,double类型,在内存中的结构(存储方式)

    C语言中float,double类型,在内存中的结构(存储方式)从存储结构和算法上来讲,double和float是一样的,不一样的地方仅仅是float是32位的,double是64位的,所以doubl ...

随机推荐

  1. [GeekBand] C++学习笔记(2)——BigThree、OOP

    本篇笔记主要分为三个部分,第一部分是以String类为例的基于对象的编程,重点在于构造与析构.拷贝构造函数.拷贝赋值函数三个重要函数.这一部分与笔记(1)中的内容结合起来就是基于对象编程的主要内容.第 ...

  2. cics下任务的停止

    cicsterm CEMT I TA TAB==找到要停止的进程 在进程后加入 f或者p 或者fp =========================== 或者找到进程ID号 用命令:kill -9 ...

  3. 水题~~~~HDU 4788

    Description Yesterday your dear cousin Coach Pang gave you a new 100MB hard disk drive (HDD) as a gi ...

  4. Json操作

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  5. Python3 面向对象

    Class 在Python中,定义类是通过class关键字: class Student(object): pass class后面紧接着是类名,即Student,类名通常是大写开头的单词,紧接着是( ...

  6. 初涉GitHub

    安装 访问https://help.github.com/articles/set-up-git/,选择对应OS平台.有文档参考,我的是OpenSuse. 在console中下载安装http://ww ...

  7. ajax 请求二进制流 图片 文件 XMLHttpRequest 请求并处理二进制流数据 之最佳实践

    写在前面 :从提出需求到完美的解决问题,实现过程是曲折的. 需求:在前(web client)后(Restful Service)端完全解耦的模式框架下,webclient需要请求 Service 返 ...

  8. spring-boot 整合redis作为数据缓存

    添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>sp ...

  9. android通过泛型获取控件或视图

    @SuppressWarnings("unchecked") public <T extends Fragment> T getFragment(int id) { T ...

  10. VB-获取某字符在其中出现的次数

    '方法1: Dim str As String " "))) '方法2: Dim n&, j& j = n = , text1.Text, "/ITEMN ...