javascript - 如何使用 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
解决方案
应该是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
推荐阅读
- fragment - sap.ui.core.Fragment.load 返回未定义
- c# - 如何使用 LINQ 在我的数据库中的调查中找到带有答案的问题数量
- python - 单击表单上的提交后未重定向到 Home.html 页面
- ruby-on-rails - 活动管理员嵌套资源表单生成
- ruby-on-rails - 如何进行安全的 Rails PayPal 支付?
- javascript - 如何使非 Firebase 用户无法访问页面?
- codeigniter - 如何使用 mvc 结构使用 codeigniter 查看更新?
- javascript - 如何将二维数组转换为对象
- unity3d - AudioSource.PlayOneShot 音量增加和削波
- node-red - 如何运行节点红色流客户端?