toFixed方法,不舍入到五位数

Akk*_*619 3 javascript floating-point jquery rounding

我有一个电话 var x = 2.305185185185195;

x = x.toFixed(5);
Run Code Online (Sandbox Code Playgroud)

x = 2.30519 但我要求不四舍五入即 2.30518

我读了一些有两位小数的线程,但找不到五个小数位。

任何帮助,将不胜感激。

Nin*_*olz 8

您可以使用适当的因子并将其下限并返回除法结果。

基本上,该解决方案将点以10 ^ d的系数向左移动,并获得该值的整数,然后将值除以前一个系数以得到右位数。

function getFlooredFixed(v, d) {
    return (Math.floor(v * Math.pow(10, d)) / Math.pow(10, d)).toFixed(d);
}

var x = 2.305185185185195;

document.write(getFlooredFixed(x, 5));
Run Code Online (Sandbox Code Playgroud)