c++ double进行精度截取 (转)

使用boost/multiprecision/cpp_dec_float.hpp

 

#include <boost/multiprecision/cpp_dec_float.hpp>

using boost::multiprecision::number;
using boost::multiprecision::cpp_dec_float_50;


int main()
{
    cpp_dec_float_50 v1("5726.867366095");    // 为防止精度丢失,只能以字符串形式赋值,
    cpp_dec_float_50 v2("5837.754018494999"); // 否则就是把错误的值赋给了对象,初值就错了
    const int64_t N = pow(10, 8);
    double newV1 = (int64_t)round(v1 * N) / double(N);
    double newV2 = (int64_t)round(v2 * N) / double(N);
    printf("%.8f\n", newV1);
    printf("%.8f\n", newV2);
}

 

posted @ 2024-04-18 22:39  鸭子船长  阅读(37)  评论(0编辑  收藏  举报