将数字四舍五入到最接近的 3 的倍数 [英] Round number up to the nearest multiple of 3

查看:32
本文介绍了将数字四舍五入到最接近的 3 的倍数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这篇关于将数字四舍五入到最接近的 3 的倍数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆