#include "iomanip" #include <iostream> using namespace std; bool to_int(double value,int & amp; res){ res=int(value); //If the result is min_int/max_int, there is a high probability that the value exceeds the limit, unless the value is exactly equal to 2147483647/-2147483648 return (res > -2147483648 & amp; & amp; res < 2147483647); } string double_to_string(double value,int decimal,bool append_zero) { //append_zero: Add 0 if there are insufficient digits // decimal Number of decimal places // return to_string(value);//can only go to the sixth position //First determine the integer part bool neg = value<0; if(value<0) value *=-1; string full_str; int full_value; if(to_int(value,full_value)){ full_str= to_string(full_value); }else{ return "ErrorBigValue";//error, value is too big } double fraction = value - full_value; int dec_avail = 16-(int)full_str.size();//16, the remaining precision of the double is the 16-int part (at least 1), which is rounded off. The excess number is not guaranteed to be correct. int decimal_rest = min(dec_avail,decimal); // Remaining available string fraction_str; if(decimal_rest>0) { if(fraction>0) { stringstream ss; ss << std::setprecision(decimal_rest) << fraction; fraction_str = ss.str(); } else if(append_zero) fraction_str="0."; } if(append_zero & amp; & amp; !fraction_str.empty() & amp; & amp; decimal>int(fraction_str.size()-2)) { decimal -= (int)fraction_str.size() - 2;//The number of 0s to be added, remove 0 and . while(true){ if(decimal>0){ fraction_str + = "0"; decimal--; }else break; } } return string(neg?"-":"") + full_str + string(fraction_str.empty()?"":fraction_str.substr(1,fraction_str.size())); } int main(){ double time1 = 1234567890.123456789123456789;// 1234567890.123457, subsequent rounding double time2 = -1234567890.123456789123456789;//-1234567890.123457, subsequent rounding double time3 = 0.123456789012345678;// 0.123456789012346, subsequent rounding double time4 = -0.123456789012345678;//-0.123456789012346, subsequent rounding double time5 = 12345678901234567895;//The number is too big double time6 = 1234567890.123456789;//1234567890.123457, subsequent rounding double time7 = -1434567892.123456789;//-1434567892.123457, subsequent rounding double time8 = -12.1;// double time9 = 13;// cout<<"time1: 20 append zero: " <<std::setprecision(30) <<double_to_string(time1,20,true)<<endl; cout<<"time1: 20 without append zero: " <<std::setprecision(30) <<double_to_string(time1,20,false)<<endl; cout<<"time1: 5 without append zero: " <<std::setprecision(30) <<double_to_string(time1,5,false)<<endl; cout<<"time1: 20 append zero: " <<std::setprecision(30) <<double_to_string(time2,20,true)<<endl; cout<<"time2: 20 without append zero: " <<std::setprecision(30) <<double_to_string(time2,20,false)<<endl; cout<<"time3: 20 append zero: " <<std::setprecision(30) <<double_to_string(time3,20,true)<<endl; cout<<"time3: 20 without append zero: " <<std::setprecision(30) <<double_to_string(time3,20,false)<<endl; cout<<"time4: 20 append zero: " <<std::setprecision(30) <<double_to_string(time4,20,true)<<endl; cout<<"time4: 20 without append zero: " <<std::setprecision(30) <<double_to_string(time4,20,false)<<endl; cout<<"time5: 20 append zero: " <<std::setprecision(30) <<double_to_string(time5,20,true)<<endl; cout<<"time6: 20 append zero: " <<std::setprecision(30) <<double_to_string(time6,20,true)<<endl; cout<<"time6: 20 without append zero: " <<std::setprecision(30) <<double_to_string(time6,20,false)<<endl; cout<<"time7: 20 append zero: " <<std::setprecision(30) <<double_to_string(time7,20,true)<<endl; cout<<"time7: 20 without append zero: " <<std::setprecision(30) <<double_to_string(time7,20,false)<<endl; cout<<"time8: 0 without append zero: " <<std::setprecision(30) <<double_to_string(time8,0,true)<<endl; cout<<"time8: 0 without append zero: " <<std::setprecision(30) <<double_to_string(time8,0, false)<<endl; cout<<"time8: 0 without append zero: " <<std::setprecision(30) <<double_to_string(time8,2,true)<<endl; cout<<"time8: 0 without append zero: " <<std::setprecision(30) <<double_to_string(time8,2,false)<<endl; cout<<"time9: 0 without append zero: " <<std::setprecision(30) <<double_to_string(time9,0,true)<<endl; cout<<"time9: 0 without append zero: " <<std::setprecision(30) <<double_to_string(time9,0, false)<<endl; cout<<"time9: 0 without append zero: " <<std::setprecision(30) <<double_to_string(time9,2,true)<<endl; cout<<"time9: 0 without append zero: " <<std::setprecision(30) <<double_to_string(time9,2,false)<<endl; return 0; }
time1: 20 append zero: 1234567890.12345700000000000000
time1: 20 without append zero: 1234567890.123457
time1: 5 without append zero: 1234567890.12346
time1: 20 append zero: -1234567890.12345700000000000000
time2: 20 without append zero: -1234567890.123457
time3: 20 append zero: 0.12345678901234600000
time3: 20 without append zero: 0.123456789012346
time4: 20 append zero: -0.12345678901234600000
time4: 20 without append zero: -0.123456789012346
time5: 20 append zero: ErrorBigValue
time6: 20 append zero: 1234567890.12345700000000000000
time6: 20 without append zero: 1234567890.123457
time7: 20 append zero: -1434567892.12345700000000000000
time7: 20 without append zero: -1434567892.123457
time8: 0 without append zero: -12
time8: 0 without append zero: -12
time8: 0 without append zero: -12.10
time8: 0 without append zero: -12.1
time9: 0 without append zero: 13
time9: 0 without append zero: 13
time9: 0 without append zero: 13.00
time9: 0 without append zero: 13