在整数末尾加0 [英] Add 0's to the end of an integer

查看:84
本文介绍了在整数末尾加0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

for(i=0; i<array.length; i++){
  sum = 4 * 5;
}

我想做的是将((array.length-1)-i)0加到sum的值上.对于此示例,假设数组长度为3.sum等于20.因此,对于循环的第一次迭代,我想将((3-1-0)-0)0加到sum的值上,因此sum为2000.下一次迭代将是((3-1-1)-1)0.因此总和等于200,依此类推.我希望我想要实现的目标是明确的.

What I'm trying to do is add ((array.length - 1) - i) 0's to the value of sum. For this example assume array length is 3. sum equals 20. So for the first iteration of the loop i want to add ((3 - 1) - 0) 0's to the value of sum, so sum would be 2000. The next iteration would be ((3 - 1) - 1) 0's. so sum would equal 200 and so on. I hope what I am trying to achieve is clear.

所以我的问题是:

是否可以仅将int移位以添加额外的数字?到目前为止,我的搜索表明事实并非如此.

Is it possible to just shift an int to add extra digits? My search thus far suggests it is not.

如果没有,我如何实现我想要的目标?

If not, how can i achieve my desired goal?

感谢您阅读我的问题,对您的帮助将不胜感激.

Thankyou for reading my question and any help would be greatly apreciated.

推荐答案

您可以将其乘以10,但是可以多次.

You can just multiply it by 10 however many times.

200 * 10 = 2000

因此,在您的情况下,必须使用for循环直到数组结尾,并在每次迭代中将 sum 相乘.但是要小心,因为 int 的最大值是2 ^ 31,所以超过它的最大值将回滚到0

So in your case, you'd have to use a for loop until the end of the array and multiply sum every iteration. Be careful though, because the max value of an int is 2^31, so it of surpasses that, it will roll back to 0

这篇关于在整数末尾加0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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