|
62 | 62 | */
|
63 | 63 | angularFilter.currency = function(amount){
|
64 | 64 | this.$element.toggleClass('ng-format-negative', amount < 0);
|
65 |
| - return '$' + angularFilter['number'].apply(this, [amount, 2]); |
| 65 | + return '$' + angularFilter.number.apply(this, [amount, 2]); |
66 | 66 | };
|
67 | 67 |
|
68 | 68 | /**
|
@@ -108,28 +108,39 @@ angularFilter.number = function(number, fractionSize){
|
108 | 108 | if (isNaN(number) || !isFinite(number)) {
|
109 | 109 | return '';
|
110 | 110 | }
|
111 |
| - fractionSize = typeof fractionSize == $undefined ? 2 : fractionSize; |
112 |
| - var isNegative = number < 0; |
113 |
| - number = Math.abs(number); |
114 |
| - var pow = Math.pow(10, fractionSize); |
115 |
| - var text = "" + Math.round(number * pow); |
116 |
| - var whole = text.substring(0, text.length - fractionSize); |
117 |
| - whole = whole || '0'; |
118 |
| - var frc = text.substring(text.length - fractionSize); |
119 |
| - text = isNegative ? '-' : ''; |
120 |
| - for (var i = 0; i < whole.length; i++) { |
| 111 | + fractionSize = isUndefined(fractionSize)? 2 : fractionSize; |
| 112 | + |
| 113 | + var isNegative = number < 0, |
| 114 | + pow = Math.pow(10, fractionSize), |
| 115 | + whole = '' + number, |
| 116 | + formatedText = '', |
| 117 | + i; |
| 118 | + |
| 119 | + if (whole.indexOf('e') > -1) return whole; |
| 120 | + |
| 121 | + number = Math.round(number * pow) / pow; |
| 122 | + fraction = ('' + number).split('.'); |
| 123 | + whole = fraction[0]; |
| 124 | + fraction = fraction[1] || ''; |
| 125 | + if (isNegative) { |
| 126 | + formatedText = '-'; |
| 127 | + whole = whole.substring(1); |
| 128 | + } |
| 129 | + |
| 130 | + |
| 131 | + for (i = 0; i < whole.length; i++) { |
121 | 132 | if ((whole.length - i)%3 === 0 && i !== 0) {
|
122 |
| - text += ','; |
| 133 | + formatedText += ','; |
123 | 134 | }
|
124 |
| - text += whole.charAt(i); |
| 135 | + formatedText += whole.charAt(i); |
125 | 136 | }
|
126 |
| - if (fractionSize > 0) { |
127 |
| - for (var j = frc.length; j < fractionSize; j++) { |
128 |
| - frc += '0'; |
| 137 | + if (fractionSize) { |
| 138 | + while(fraction.length < fractionSize) { |
| 139 | + fraction += '0'; |
129 | 140 | }
|
130 |
| - text += '.' + frc.substring(0, fractionSize); |
| 141 | + formatedText += '.' + fraction.substring(0, fractionSize); |
131 | 142 | }
|
132 |
| - return text; |
| 143 | + return formatedText; |
133 | 144 | };
|
134 | 145 |
|
135 | 146 |
|
|
0 commit comments