toFixed方法没有舍入到五位数 [英] toFixed method without rounding to five digit

查看:93
本文介绍了toFixed方法没有舍入到五位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数字 var x = 2.305185185185195;

x = x.toFixed(5);

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

x = 2.30519 but I require this without rounding i.e. 2.30518

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

I read some thread with two decimal places but could not find for five decimal places.

任何帮助都将不胜感激。

Any help would be appreciated.

推荐答案

你可以使用适当的因子并将其归结并返回除法的结果。

You can use an apropriate factor and floor it and return the result of the division.

基本上这个解决方案将点移动到左边,系数为10 ^ d并得到一个整数并将该值除以前一个因子以得到正确的数字。

Basically this solution moves the point to the left with a factor of 10^d and gets an integer of that and divided the value with the former factor to get the right digits.

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));

这篇关于toFixed方法没有舍入到五位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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