首页 > 解决方案 > 如何使用 math.ceil 获取 Javascript 中的前四个十进制数字?

问题描述

目前有以下javascript代码:

div.html( Drupal.t("Date")+": "+d.date+"</br>"+Drupal.t("Value")+": " + (d.value))

我得到的价值是 0.1651859999999985 。我想将它四舍五入到前四位小数。有什么帮助吗?我试过Math.ceil(d.value*10)/10)了,但它只得到第一个小数:(有什么帮助吗?我想得到 0.1652

标签: javascript

解决方案


应该是Math.ceil(d.value*10000)/10000 例子:

console.log(Math.ceil(0.1683123*10000)/10000)
console.log(Math.ceil(823.124148336763*10000)/10000) // round up to the ceiling
console.log(Math.floor(823.124148336763*10000)/10000) // round down to the floor
console.log(Math.round(823.124148336763*10000)/10000) // below .5 -> down, above .5 -> up


推荐阅读