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";
}

相关文章:

  • 2022-12-23
  • 2021-08-11
  • 2021-05-22
  • 2022-12-23
  • 2023-03-16
  • 2022-12-23
  • 2021-09-10
猜你喜欢
  • 2021-12-06
  • 2021-09-13
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-19
  • 2022-12-23
相关资源
相似解决方案