围绕n*10的算法

Lud*_*udo 0 javascript algorithm math

我试图找到一个算法来结果:

45 => 100
458 => 1000
8456 => 10000
Run Code Online (Sandbox Code Playgroud)

我想以这种方式"围绕",我调查Math.round()但是它认为不够.

Mat*_*igh 8

对于单行代码,您可以执行Rup所说的内容.基数10日志将为您提供最接近的10次幂,然后对日志结果的上限值进行10次

var x = 45;

Math.pow(10, Math.ceil(Math.log(x) / Math.LN10));
Run Code Online (Sandbox Code Playgroud)