This is my custom Currency Mask for Angular JS Directive.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
.directive('currencyMask', function() { return { restrict: 'A', require: 'ngModel', link: function (scope, elem, attrs, controller) { elem.bind('blur', function() { elem.val(formatNumberWithCommasAndDecimal(scope.$eval(attrs.ngModel), true)); }); controller.$parsers.push(function(value) { if (value) { value = formatNumberWithCommasAndDecimal(value, false); controller.$setViewValue(value); controller.$render(); } return value; }); controller.$formatters.push(function(value) { if (value) value = formatNumberWithCommasAndDecimal(value, true); return value; }); } }; }); function formatNumberWithCommasAndDecimal(value, addExtraZero) { if (!value) return; if (addExtraZero == undefined) addExtraZero = false; value = value.toString(); value = value.replace(/,/g, '').replace(/[^1-9][^0-9\.]/g, ''); // parse[0] are digits. parse[1] is decimal var parts = value.split('.'); if (parts.length > 2) { // if user puts in more dots, merge them again but remove other dots except first one found, // then split again dummy = ''; for (i=0; i<parts.length; i++) { dummy += parts[i]; if (i == 1) dummy += '.'; } parts = dummy.split('.'); } // if there are trailing 0s at the beginning, convert to float to remove them // then convert back to string parts[0] = parseFloat(parts[0]) + ''; // parse by thousand adding comma var rev = parts[0].split("").reverse().join(""); var dum = ''; for (var i=0; i<rev.length; i++) { if (i > 0 && i % 3 == 0) { dum += ','; } dum += rev.charAt(i); } if (dum.length > 0) parts[0] = dum.split("").reverse().join(""); if (parts[1] && parts[1].length > 2) { parts[1] = parts[1].substring(0, 2); } // if no decimal found but dot is present if (addExtraZero) { if (parts[1]) { if (parts[1] == '') parts[1] = '00'; else if (parts[1].length == 1) parts[1] += '0'; } else { if (parts[0].length > 0) parts[0] += '.00'; } } return parts.join('.'); } |