C++ double转string

#include "iomanip"
#include <iostream>

using namespace std;


bool to_int(double value,int& res){
    res=int(value);
    //如果结果是min_int/max_int 大概率就是value超过限度了,除非value刚好等于2147483647/-2147483648
    return (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 "ErrorBigValue";//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 = min(dec_avail,decimal);// 剩余可用的
    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 && !fraction_str.empty() && decimal>int(fraction_str.size()-2))
    {
        decimal -= (int)fraction_str.size() - 2;//需要添加的0的数量, 去除 0 和 .
        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, 后面的四舍五入
    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, 后面的四舍五入

    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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值