c++ 保留小数

1.    四舍五入格式化输出。

例如:

double i = 123.456四舍五入保留两位小数

cout << setiosflags(ios::fixed) << setprecision(2) << i << endl;

输出结果:123.46


2.    截断要保留位数后面的小数

例如:

double i = 123.456截断保留两位小数

    1)将i转为string后判断小数点位置,在保留其后面两位。

    2)将i*100,转为整数,然后转为double,除以100

具体代码以后再加上

输出结果:123.45




LOFTER:hgfalgorithm   http://hgfal.lofter.com/post/28eef2_ebf087
原文地址:https://www.cnblogs.com/hgfgood/p/4248330.html