jQuery限制为2个小数位 [英] jQuery limit to 2 decimal places

查看:284
本文介绍了jQuery限制为2个小数位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
JavaScript:使用数字精确地设置两个小数位

Possible Duplicate:
JavaScript: formatting number with exactly two decimals

如何限制以下jQuery返回2个小数位?

How do I limit the following jQuery return to 2 decimal places?

$("#diskamountUnit").val('$' + $("#disk").slider("value") * 1.60);

我认为我必须将toFixed(2)扔到那里,但是我似乎无法正确地进行订购.

I figure I've got to throw toFixed(2) somewhere into there, but I can't seem to get the ordering right or something.

推荐答案

在设置#diskamountUnit元素值时,可以使用变量进行计算并使用toFixed:

You could use a variable to make the calculation and use toFixed when you set the #diskamountUnit element value:

var amount = $("#disk").slider("value") * 1.60;
$("#diskamountUnit").val('$' + amount.toFixed(2));

您也可以在val方法调用中一步完成此操作,但是IMO第一种方法更具可读性:

You can also do that in one step, in the val method call but IMO the first way is more readable:

$("#diskamountUnit").val('$' + ($("#disk").slider("value") * 1.60).toFixed(2));

这篇关于jQuery限制为2个小数位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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