如何四舍五入到指定数字的最接近倍数?

编程入门 行业动态 更新时间:2024-10-14 04:24:29
本文介绍了如何四舍五入到指定数字的最接近倍数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经看过很多关于将数字四舍五入到最接近的倍数的问题,但是我对它们的方法不够了解,无法采用它们四舍五入到45,或者他们使用其他语言的特定语言方法。

I've looked at many questions regarding rounding up to the nearest multiple of a number, but I can't understand their methods well enough to adopt them for rounding up to 45 or they use language specific methods of other languages.

如果上述内容还没有多大意义,这里有一些更详细的解释:

Here is a bit more detailed explanation if the above doesn't make much sense yet:

示例输入:

int num1 = 67; int num2 = 43;

预期结果:

>>round(num1) = 90 >>round(num2) = 45

推荐答案

足以添加缺少的 mod45 :

int upperround45(int i) { int temp = i%45; //For the algorithm we wish the rest to be how much more than last multiple of 45. if (temp < 0 ) temp = 45 + temp; if (temp == 0) { return i; } else { return i + 45 - temp; } }

编辑:

通常:

int upperround(int num, int base) { int temp = num%base; if (temp < 0 ) temp = base + temp; if (temp == 0) return num; return num + base - temp;

更多推荐

如何四舍五入到指定数字的最接近倍数?

本文发布于:2023-11-07 23:26:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1567719.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:倍数   最接近   四舍五入   数字

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!