浙江省高等学校教师教育理论培训

微信搜索“毛凌志岗前心得”小程序

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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";
}
posted on 2014-07-02 14:42  lexus  阅读(756)  评论(0编辑  收藏  举报