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

查看:36
本文介绍了逗号后将数字四舍五入为 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?

推荐答案

EDIT 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(roundedString); // 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(因此绝对值在 0.5 边界处舍入时增加)

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

jsFiddle 示例

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

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