获得浮点数的小数部分
float n=12.223f;
int i=(int)n;
float x=n-(float)i;
x得到的数不是0.223
[解决办法]
有图有真相!
#include<stdio.h>#include<iostream>using namespace std;int main(){ float n=12.223f; int i=(int)n; float x=n-(float)i; cout<<x; return 0;}
[解决办法]
这是因为对有些小数,浮点数是不能精确表示的,自己去百度浮点数精度就知道了