如何在javascript中解析带有两个小数位的浮点数? [英] How to parse float with two decimal places in javascript?
本文介绍了如何在javascript中解析带有两个小数位的浮点数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码。我想这样,如果price_result等于一个整数,让我们说10,那么我想添加两个小数位。所以10将是10.00。
或者等于10.6将是10.60。不知道怎么做。
I have the following code. I would like to have it such that if price_result equals an integer, let's say 10, then I would like to add two decimal places. So 10 would be 10.00. Or if it equals 10.6 would be 10.60. Not sure how to do this.
price_result = parseFloat(test_var.split('$')[1].slice(0,-1));
推荐答案
您可以使用 toFixed()来做那件事
var twoPlacedFloat = parseFloat(yourString).toFixed(2)
这篇关于如何在javascript中解析带有两个小数位的浮点数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文