#include
#include
using namespace std;
string flot2str(float f)
{
if (f == 0)
return "0";
string s;
int x = (int)f;
int d;
while (x > 0)
{
d = x % 10;
x /= 10;
s = (char)(d + '0') + s;
}
s += ".";
float y = f - (int)f;
while (y > 0)
{
y *= 10;
d = (int)y;
s += (char)(d + '0');
y -= d;
}
return s;
}
int main()
{
float f = 0;
cout << flot2str(f);
system("pause");
return 0;
}
由于float的精度问题,并不是很准确,应该有更好的方法,我觉得可以参考一下printf的实现。
相关文章: