1 | var toNumber = require('../lang/toNumber');
|
2 |
|
3 | |
4 |
|
5 |
|
6 | function currencyFormat(val, nDecimalDigits, decimalSeparator, thousandsSeparator) {
|
7 | val = toNumber(val);
|
8 | nDecimalDigits = nDecimalDigits == null? 2 : nDecimalDigits;
|
9 | decimalSeparator = decimalSeparator == null? '.' : decimalSeparator;
|
10 | thousandsSeparator = thousandsSeparator == null? ',' : thousandsSeparator;
|
11 |
|
12 |
|
13 |
|
14 | var fixed = val.toFixed(nDecimalDigits),
|
15 |
|
16 | parts = new RegExp('^(-?\\d{1,3})((?:\\d{3})+)(\\.(\\d{'+ nDecimalDigits +'}))?$').exec( fixed );
|
17 |
|
18 | if(parts){
|
19 | return parts[1] + parts[2].replace(/\d{3}/g, thousandsSeparator + '$&') + (parts[4] ? decimalSeparator + parts[4] : '');
|
20 | }else{
|
21 | return fixed.replace('.', decimalSeparator);
|
22 | }
|
23 | }
|
24 |
|
25 | module.exports = currencyFormat;
|
26 |
|
27 |
|