作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
当 float 为整数时,如何将 float 转为字符串并加上2位小数。目前我正在使用以下内容,但对于整数,我想添加 .00。我需要为此编写自己的例程吗?
float floatingNumber = 1.00;
string string;
ostringstream stream;
stream << floatingNumber;
string += stream.str(); // result is 1 not 1.00
最佳答案
您应该手动设置精度并使用标志,这样您就可以使用固定表示法
stream << std::fixed << std::setprecision(2) << floatingNumber;
关于c++ - 如何将 float 转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16582640/
我是一名优秀的程序员,十分优秀!