#include "iomanip"
#include <iostream>using namespace std;bool to_int(double value,int& res){res=int(value);//如果结果是min_int/max_int 大概率就是value超过限度了,除非value刚好等于2147483647/-2147483648return (res > -2147483648 && res < 2147483647);
}string double_to_string(double value,int decimal,bool append_zero) {//append_zero:位数不足的补0// decimal 小数点后几位
// return to_string(value);//只能到第六位//先确定整数的部分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 "error,value is too big";//error,value is too big}double fraction = value - full_value;int dec_avail = 16-(int)full_str.size();//16,double的剩余精度16-int的部分(至少是1)后面四舍五入 ,超过的部分数字不能保证正确int decimal_rest = max(0,min(dec_avail,decimal));// 剩余可用的string fraction_str;stringstream ss;ss << std::setprecision(decimal_rest) << fraction;fraction_str = ss.str();if(append_zero){decimal -= (int)fraction_str.size() - 2;//需要添加的0的数量, 去除 0 和 .while(true){if(decimal>0){fraction_str += "0";decimal--;}elsebreak;}}if(neg)return "-"+full_str+fraction_str.substr(1,fraction_str.size());elsereturn full_str+fraction_str.substr(1,fraction_str.size());
}int main(){double time1 = 1234567890.123456789123456789;// 1234567890.123457, 后面的四舍五入double time2 = -1234567890.123456789123456789;//-1234567890.123457, 后面的四舍五入double time3 = 0.123456789012345678;// 0.123456789012346, 后面的四舍五入double time4 = -0.123456789012345678;//-0.123456789012346, 后面的四舍五入double time5 = 12345678901234567895;// 数字太大double time6 = 1234567890.123456789;//1234567890.123457, 后面的四舍五入double time7 = -1434567892.123456789;//-1434567892.123457, 后面的四舍五入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;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: error,value is too big
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