如何将数字格式化为小数点后两位,但前提是已经有小数位?
我有一个 jQuery 1.5+ 脚本,您在下拉菜单中选择一个数量(1、2、3 等),它会将该数量乘以 1.50 美元以显示总价。基本上 - 它将所选数量(1、2、3 等)乘以 1.50 美元的基本价格 - 但是 - 我不知道如何用小数正确显示价格 - 例如:如果您选择数量 2,价格正确显示为 $3(无小数)。但是,如果您选择 1 或 3,价格将显示为 1.5 美元/4.5 美元 - 缺少小数点后百分之一的 0。
这是代码 - 知道如何在没有两位小数的情况下显示第二个 0 吗? $3 应保持为 $3,但 $4.5 应变为 $4.50,等等 - 如果不将所有数字显示为小数点后两位,我就无法让它工作,这就是我被困住的地方!
<script type='text/javascript'>
$(function() {
$('#myQuantity').change(function() {
var x = $(this).val();
$('#myAmount').text('$'+(x*1.5));// this is the part that isn't displaying decimals correctly!
});
});
</script>
我正在尝试类似 result = num.toFixed(2); 的东西但还无法让它发挥作用。
谢谢您!
I have a jQuery 1.5+ script, and you select a quantity in a drop-down menu (1,2,3, etc) and it multiplies that quantity by $1.50 to show you a total price. Basically - it's multiplying the quantity selected (1, 2, 3, etc) by the base price of $1.50 - BUT - I can't figure out how to display the price correctly with decimals - example: if you select a quantity of 2, the price displays correctly as $3 (no decimals). But, if you choose 1, or 3, the price displays as $1.5 / $4.5 - missing a 0 in the hundredths decimal place.
Here's the code - any idea how to show a second 0 in the case that there are not already two decimals? $3 should stay as $3, but $4.5 should become $4.50, etc - I can't get it to work without showing ALL numbers to two decimals, and that's where I'm stuck!
<script type='text/javascript'>
$(function() {
$('#myQuantity').change(function() {
var x = $(this).val();
$('#myAmount').text('
I'm experimenting with something like result = num.toFixed(2); but can't get it to work yet.
Thank you Kindly!
+(x*1.5));// this is the part that isn't displaying decimals correctly!
});
});
</script>
I'm experimenting with something like result = num.toFixed(2); but can't get it to work yet.
Thank you Kindly!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
这应该可以完成这项工作:
This should do the job:
我建议:
它会自动添加 0、1 或 2 位小数。
I suggest:
It automatically adds 0, 1 or 2 decimal places.
工作示例: http://jsfiddle.net/peeter/JxPZH/
Working example: http://jsfiddle.net/peeter/JxPZH/
怎么样
How about
有了这个,您可以将数字转换为正确的小数位数,并且在转换回数字时,去掉所有无用的零。
With this you can cast your number to the correct amount of decimal places and, when converting back to Number, get rid of all useless zeros.
使用这个函数:
Use this function:
如果数字%1 不返回零,则它不是整数。
if a number%1 does not return zero, it is not an integer.
parseFloat(Number(25).toFixed(2))
`
parseFloat(Number(25).toFixed(2))
`
如果您想要更通用的解决方案,请基于 odrm 想法。
If you want more generic solution, based on odrm idea.
您可以只使用这个函数,并在任何您想要的地方调用它
You can just use this function, and call it wherever you want to
我建议使用内置数字格式化对象的 Javascript。
https://developer.mozilla.org/ en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat
I would suggest using Javascripts built in number formatting object.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat
另一种可能性是,将字符串强制转换为数字,并利用 0.00 为 false 的事实来确定其显示方式:
其中:
One other possibility, coerce the string into a number and leverage the fact that 0.00 is falsey to determine how it displays:
Where as:
实际上有一个更加动态的解决方案来实现这一目标。这在您的位数发生变化时尤其有用。
这最终将是这样的:
There's actually a much more dynamic solution to achieve this. This is useful especially when your number of digits will change.
This will end up to this:
我建议:
I would suggest: