逗号后将数字舍入为2位数 [英] Rounding numbers to 2 digits after comma

查看:208
本文介绍了逗号后将数字舍入为2位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道怎么做?我正在添加逗号,结果当然总是一个数字,逗号后面的数字太多了。任何人?

I have no idea how to do this? I'm adding comma numbers, result is of course always a number with way too many digits after the comma. anyone?

推荐答案

编辑2

使用 Number 对象的 toFixed 方法,如下所示:

Use the Number object's toFixed method like this:

var num = Number(0.005) // The Number() only visualizes the type and is not needed
var roundedString = num.toFixed(2);
var rounded = Number(roudedString); // toFixed() returns a string (often suitable for printing already)

它将42.0054321舍入为42.01

It rounds 42.0054321 to 42.01

它向下舍入0.005到0.01

It rounds 0.005 to 0.01

它将-0.005舍入到-0.01(所以在舍入时绝对值会增加.5边框)

It rounds -0.005 to -0.01 (So the absolute value increases on rounding at .5 border)

jsFiddle示例

这篇关于逗号后将数字舍入为2位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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