如何找到10个任意整数的倍数? [英] How do I find the next multiple of 10 of any integer?

查看:264
本文介绍了如何找到10个任意整数的倍数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

动态整数将是任何数字从0到150。

即。 - 数返回41,需要回到50。如果数为10需要返回10号是1需要返回10

想我可以使用吊装功能,如果我修改的整数为十进制...?然后用取整函数,并把回十进制?
唯一的是还必须知道,如果数目为1,2或3个数字(即 - 7 VS 94 VS 136)

有没有更好的方式来实现这一目标?

感谢你,

解决方案

  N +(10  -  N%10)
 

如何工作的。 %运算符的计算结果为除法的余数(所以 41%,10 的计算结果为1,而 45%,10 的计算结果〜5)。减去从10计算多少你需要多少到达下一个倍数。

唯一的问题是,这会变成40到50。如果你不希望出现这种情况,你需要添加一个检查,以确保它不是已经是10的倍数。

 如果(N%10)
    N = N +(10  -  N%10);
 

Dynamic integer will be any number from 0 to 150.

i.e. - number returns 41, need to return 50. If number is 10 need to return 10. Number is 1 need to return 10.

Was thinking I could use the ceiling function if I modify the integer as a decimal...? then use ceiling function, and put back to decimal?
Only thing is would also have to know if the number is 1, 2 or 3 digits (i.e. - 7 vs 94 vs 136)

Is there a better way to achieve this?

Thank You,

解决方案

n + (10 - n % 10)

How this works. The % operator evaluates to the remainder of the division (so 41 % 10 evaluates to 1, while 45 % 10 evaluates to 5). Subtracting that from 10 evaluates to how much how much you need to reach the next multiple.

The only issue is that this will turn 40 into 50. If you don't want that, you would need to add a check to make sure it's not already a multiple of 10.

if (n % 10)
    n = n + (10 - n % 10);

这篇关于如何找到10个任意整数的倍数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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