gpt4 book ai didi

javascript - 将数字舍入到最接近的 3 的倍数

转载 作者:IT王子 更新时间:2023-10-29 03:15:03 24 4
gpt4 key购买 nike

我如何将数字四舍五入到最接近 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;

关于javascript - 将数字舍入到最接近的 3 的倍数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3254047/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com