实现方法:
var sum = 6260
var num = parseFloat(sum)
if (!isNaN(num)) {
num = Math.round(num * 100) / 100
var newSum = num.toLocaleString()
var resi = newSum .indexOf('.')
if (resi < 0) {
resi = newSum .length
newSum += '.'
}
while (newSum .length <= rs + 2) {
newSum += '0'
}
//console.log(newSum )---6,260.00
}
var num1 = parseFloat(row.distance)
if (!isNaN(num1)) {
num1 = Math.round(num1 * 100) / 100
var str = num1.toLocaleString()
var rs = str.indexOf('.')
if (rs < 0) {
rs = str.length
str += '.'
}
while (str.length <= rs + 2) {
str += '0'
}
}
return row.distance === null ? '-' : str
效果图:
原始数据有小数则保留后两位,否则四舍五入,自动补0.
追加一个简洁的方法:
(Math.round(num * 100) / 100).toLocaleString()
即转为货币格式又保留两位小数,亲测有效。