js:toFix方法为银行家算法,0.15会算成0.01,所以舍弃
var num =2.446242342;
num = Math.round((num + Number.EPSILON) * 100) / 100; // 输出结果为 2.45
wxs:
var toFixedFn = function (x) {
var f = parseFloat(x);
if (isNaN(f)) {
return '0.00';
}
var f = Math.round(x * 100) / 100;
var s = f.toString();
var rs = s.indexOf('.');
if (rs < 0) {
rs = s.length;
s += '.';
}
while (s.length <= rs + 2) {
s += '0';
}
return s
}
使用示例:
<wxs module="m1">
var toFixedFn = function (x) {
var f = parseFloat(x);
if (isNaN(f)) {
return '0.00';
}
var f = Math.round(x * 100) / 100;
var s = f.toString();
var rs = s.indexOf('.');
if (rs < 0) {
rs = s.length;
s += '.';
}
while (s.length <= rs + 2) {
s += '0';
}
return Number(s).toFixed(2);
}
module.exports = {
toFixedFn: toFixedFn
};
</wxs>