将数字四舍五入为10 ^

时间:2018-07-24 09:53:41

标签: java math random numbers

我有一个随机数,例如35 127 3658 45782等...我想将它们四舍五入为10 ^形式,例如10 100 100010000。我可以使用此代码来做到这一点:

Math.pow(10, (int)(Math.log10(number)) + 1);

但是对于我来说,这段代码对于这样的基本操作来说似乎有点复杂和漫长。有更好的方法吗?

1 个答案:

答案 0 :(得分:-2)

float t =10,number;
//scan a value for "number"
while(number/t > 1)
{
t = t * 10;
}
if( (number/t) % 1 >= 0.5)
System.out.println(t);
else
System.out.println(t/10);

Though it takes more lines, this is simple to understand.