Limiting double to 3 decimal places

前端 未结 8 2238
小鲜肉
小鲜肉 2020-11-27 04:38

This i what I am trying to achieve:

If a double has more than 3 decimal places, I want to truncate any decimal places beyond the third. (do not round.)



        
8条回答
  •  广开言路
    2020-11-27 04:59

    In C lang:

    double truncKeepDecimalPlaces(double value, int numDecimals)
    {
        int x = pow(10, numDecimals);
        return (double)trunc(value * x) / x;
    }
    

提交回复
热议问题