保留double数据的n个有效位C++

template <typename T1, typename T2>
inline double setDoublePrecicious(const T1 &number, const T2 &n){

   stringstream str_number;
   str_number<<std::setprecision(n)<<number;
   std::string string_number = str_number.str();

   str_number.clear();
   str_number.str("");
   return std::atof(string_number.c_str());

}
posted @ 2022-05-31 19:05  小小灰迪  阅读(135)  评论(0编辑  收藏  举报