E75*_*E75 1 javascript math performance rounding
我想在随机十进制值后舍入该值
Example(round up when value > x.90):
18.25478 => 18
18.7545 => 18
18.90 => 19
18.95 = > 19
Run Code Online (Sandbox Code Playgroud)
我知道Math.ceil和Math.Floor方法,但我想结合一种方法.而且我还读到Math.floor和ceil很慢的值(我要在列表中转换3000.000+值!)
我怎样才能在JavaScript中执行此操作?
你可以添加0.1和使用Math.floor.
function round(v) {
return Math.floor(v + 0.1);
}
var array = [
18.25478, // => 18
18.7545, // => 18
18.90, // => 19
18.95, // => 19
];
console.log(array.map(round));Run Code Online (Sandbox Code Playgroud)