zoukankan      html  css  js  c++  java
  • 字符串转double的精度问题

    搞了半天才发现是字符串转小数位数的问题,总是丢位

    void string2double(string a, double &b)
    {
            istringstream iss;//istringstream从string读入,和cin一样仅仅重载了>>,可以把string转为int
            iss.clear();//每次使用前先清空
            iss.str(a);
            iss>>b;//将输入流中的内容写入到int n,
    }

    我想保留小数点后边10位这程序就不行了。无奈自己写一个:

    最后发现不是上述程序的问题,而是显示的问题,呵呵。搞了一个下午。

    #include <iostream>
    #include <sstream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    
    void string2int(string a, double &b)
    {
            istringstream iss;//istringstream从string读入,和cin一样仅仅重载了>>,可以把string转为int
            iss.clear();//每次使用前先清空
            iss.str(a);
            iss>>b;//将输入流中的内容写入到int n,
    }
    
    int main()
    {
        string a = "0.0032478965394";
        double b;
        string2int(a,b);
        cout << b << endl;
        cout.precision(15);
        cout << b << endl;
    }
    
    //output
    /*
    *0.0032479
    *0.0032478965394
    */
    The Safest Way to Get what you Want is to Try and Deserve What you Want.
  • 相关阅读:
    使用合理jQuery选择器查找DOM元素
    DOM对象和jQuery对象
    jQuery实现返回顶部
    行内元素,块级元素
    图片自适应缩放
    幽灵按钮
    background-attachment:fixed
    RegExp
    正则
    Date
  • 原文地址:https://www.cnblogs.com/Shinered/p/9380553.html
Copyright © 2011-2022 走看看