modf returns 1 as the fractional:

前端 未结 2 1825
别跟我提以往
别跟我提以往 2021-01-24 05:17

I have this static method, it receives a double and \"cuts\" its fractional tail leaving two digits after the dot. works almost all the time. I have noticed that when i

2条回答
  •  我在风中等你
    2021-01-24 05:39

    Here is a way without rounding:

    double double_cut(double d)
    {
        long long x = d * 100;
        return x/100.0;
    }
    

    Even if you want rounding according to 3rd digit after decimal point, here is a solution:

    double double_cut_round(double d)
    {
        long long x = d * 1000;
    
        if (x > 0)
            x += 5;
        else
            x -= 5;
    
        return x / 1000.0;
    }
    

提交回复
热议问题