码迷,mamicode.com
首页 > 编程语言 > 详细

C++ double类型转string类型后,怎么实现小数点后只显示一个数字

时间:2014-07-07 12:05:31      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:http   color   width   os   html   io   

C++ double类型转string类型后,怎么实现小数点后只显示一个数字

#include <iostream>
#include <sstream>
#include <iomanip>
??
template <class T>
std::string fmt(T in, int width = 0, int prec = 0) {
????std::ostringstream s;
????s << std::setw(width) << std::setprecision(prec) << in;
????return s.str();
}
??
int main(){
????std::string s = fmt(66.0 / 30.0, 2, 2);
????std::cout << s << "\n";
}

C++ double类型转string类型后,怎么实现小数点后只显示一个数字,布布扣,bubuko.com

C++ double类型转string类型后,怎么实现小数点后只显示一个数字

标签:http   color   width   os   html   io   

原文地址:http://www.cnblogs.com/lexus/p/3820220.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!