将数字四舍五入到最接近的 3 的倍数

2022-01-14 00:00:00 integer rounding javascript

嘿,我将如何将一个数字四舍五入到最接近 3 的倍数?

Hay, how would i go about rounded a number up the nearest multiple of 3?

25 would return 27
1 would return 3
0 would return 3
6 would return 6

谢谢

推荐答案

    if(n > 0)
        return Math.ceil(n/3.0) * 3;
    else if( n < 0)
        return Math.floor(n/3.0) * 3;
    else
        return 3;

相关文章