强制两位小数(包括填充0)

时间:2018-02-28 21:08:29

标签: javascript numbers decimal currency

我在这里基本上尝试过所有事情:JavaScript math, round to two decimal places

我不仅要将其减少到两位小数,而且如果它减少了我需要添加零。我在下面的示例中使用.1



var base = .1;  //desired result is 0.10
var total;

// the x100/100 method
total = (base * 100) / 100;
console.log(total);  //outputs 0.1

// the .toFixed method
total = (base).toFixed(2);
console.log(total + " (" + typeof total + ")"); //looks right, but it's a string

// unary +()
total = +((base).toFixed(2));
console.log(total); //outputs .1




0 个答案:

没有答案