|
34 | 34 | */
|
35 | 35 | angularFilter.currency = function(amount){
|
36 | 36 | this.$element.toggleClass('ng-format-negative', amount < 0);
|
37 |
| - return '$' + angularFilter['number'].apply(this, [amount, 2]); |
| 37 | + return '$' + angularFilter.number.apply(this, [amount, 2]); |
38 | 38 | };
|
39 | 39 |
|
40 | 40 | /**
|
@@ -80,28 +80,39 @@ angularFilter.number = function(number, fractionSize){
|
80 | 80 | if (isNaN(number) || !isFinite(number)) {
|
81 | 81 | return '';
|
82 | 82 | }
|
83 |
| - fractionSize = typeof fractionSize == $undefined ? 2 : fractionSize; |
84 |
| - var isNegative = number < 0; |
85 |
| - number = Math.abs(number); |
86 |
| - var pow = Math.pow(10, fractionSize); |
87 |
| - var text = "" + Math.round(number * pow); |
88 |
| - var whole = text.substring(0, text.length - fractionSize); |
89 |
| - whole = whole || '0'; |
90 |
| - var frc = text.substring(text.length - fractionSize); |
91 |
| - text = isNegative ? '-' : ''; |
92 |
| - for (var i = 0; i < whole.length; i++) { |
| 83 | + fractionSize = fractionSize == undefined ? 2 : fractionSize; |
| 84 | + |
| 85 | + var isNegative = number < 0, |
| 86 | + pow = Math.pow(10, fractionSize), |
| 87 | + whole = '' + number, |
| 88 | + formatedText = '', |
| 89 | + i; |
| 90 | + |
| 91 | + if (whole.indexOf('e') > -1) return whole; |
| 92 | + |
| 93 | + number = Math.round(number * pow) / pow; |
| 94 | + fraction = ('' + number).split('.'); |
| 95 | + whole = fraction[0]; |
| 96 | + fraction = fraction[1] || ''; |
| 97 | + if (isNegative) { |
| 98 | + formatedText = '-'; |
| 99 | + whole = whole.substring(1); |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | + for (i = 0; i < whole.length; i++) { |
93 | 104 | if ((whole.length - i)%3 === 0 && i !== 0) {
|
94 |
| - text += ','; |
| 105 | + formatedText += ','; |
95 | 106 | }
|
96 |
| - text += whole.charAt(i); |
| 107 | + formatedText += whole.charAt(i); |
97 | 108 | }
|
98 |
| - if (fractionSize > 0) { |
99 |
| - for (var j = frc.length; j < fractionSize; j++) { |
100 |
| - frc += '0'; |
| 109 | + if (fractionSize) { |
| 110 | + while(fraction.length < fractionSize) { |
| 111 | + fraction += '0'; |
101 | 112 | }
|
102 |
| - text += '.' + frc.substring(0, fractionSize); |
| 113 | + formatedText += '.' + fraction.substring(0, fractionSize); |
103 | 114 | }
|
104 |
| - return text; |
| 115 | + return formatedText; |
105 | 116 | };
|
106 | 117 |
|
107 | 118 |
|
|
0 commit comments