123456789101112131415 |
- function money(s, n) {
- n = n || 0;
- n = n >= 0 && n <= 5 ? n : 2;
- s = parseFloat((s + '').replace(/[^\d\.-]/g, '')).toFixed(n) + '';
- s = s.split('.');
- let l = s[0].split('').reverse(),
- r = s[1] || '',
- t = '';
- for (let i = 0; i < l.length; i++) {
- t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? ',' : '');
- }
- return t.split('').reverse().join('') + (r ? ('.' + r) : r);
- }
- module.exports = money;
|