資料來自於 http://jashliao.pixnet.net/blog/post/223492566-c-c++-%E6%B5%AE%E9%BB%9E%E6%95%B8%5B%E7%B2%BE%E5%BA%A6%E5%95%8F%E9%A1%8C%5D~%E5%9B%9B%E6%8D%A8%E4%BA%94%E5%85%A5%E5%87%BD%E6%95%B8

// 四捨五入 取到 小數點第 X 位
double CLib::rounding(double num, int index)
{
    //https://dotblogs.com.tw/forloop/2016/07/31/rounding
    bool isNegative = false; // whether is negative number or not

    if(num < 0) // if this number is negative, then convert to positive number
    {
        isNegative = true;
        num = -num;
    }

    if(index >= 0)
    {
        int multiplier;
        multiplier = pow(10, index);
        num = (int)(num * multiplier + 0.5) / (multiplier * 1.0);
    }

    if(isNegative) // if this number is negative, then convert to negative number
    {
        num = -num;
    }

    return num;
}

arrow
arrow

    痞客興 發表在 痞客邦 留言(0) 人氣()