舍入数字最接近3的倍数 [英] Round number up to the nearest multiple of 3
本文介绍了舍入数字最接近3的倍数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hay,我怎样才能将数字四舍五入到最接近的3的倍数?
Hay, how would i go about rounded a number up the nearest multiple of 3?
ie
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屋!
查看全文