如何在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屋!
查看全文