function transformToCurrency(num) {
let [integer, decimal] = (Math.round(num*100)/100 + '').split('.')
let result = '', reverseInt = integer.split('').reverse()
for(let i = 0;i < reverseInt.length; i ++) {
result += reverseInt[i] + ((i+1)%3 === 0 && (i + 1) !== reverseInt.length ? ',' : '')
}
return result.split('').reverse().join('') + '.' + decimal
}
网友评论