1

I'm using the ng-currency library - http://aguirrel.github.io/ng-currency/ - to display currency values in a project. However, it's not exactly what I need, the currency value

  • cannot be negative
  • cannot be greater than 999,999,999
  • must be rounded to the nearest whole number i.e. contain no decimal
  • rendered as a currency value, i.e. 1234 is displayed as $1234.00

I've forked the project here - https://github.com/tetsujin1979/ng-currency/blob/master/src/ng-currency.js I removed the minus symbol from the regular expressions to stop users entering negative values, and edited the on blur method to round the figures before displaying them, as shown below, but when the rounded value is greater than $999,999.00, the model isn't getting the assigned values

element.on("blur", function () {
    var roundedValue = Math.round(ngModel.$modelValue);
    if(roundedValue > 999999999) {
    roundedValue = 999999999;
    }
    ngModel.$setViewValue(roundedValue);
    element.val($filter('currency')(roundedValue, currencySymbol()));
    ngModel.$render();
});

I've created a plunkr here to demonstrate this here: http://plnkr.co/Y7WkJtjlDZF3pXo84MRs

Joseph McCarthy
  • 897
  • 2
  • 19
  • 41

2 Answers2

1

From your plunker I see you are mixing versions:

From https://github.com/aguirrel/ng-currency#versions

Versions

If you use angular 1.2.x please, use 0.7.x version. If you use angular 1.3.x or above just use 0.8.x version instead.

Please, use https://rawgit.com/aguirrel/ng-currency/v0.8.x/src/ng-currency.js instead of uploaded ng-currency.js in you plunkr.

  <script src="https://rawgit.com/aguirrel/ng-currency/v0.8.x/src/ng-currency.js"></script>

You can see it in action using (please add http:// as I have not more than 10 reputation) plnkr.co/edit/lH9mKrnRZZZAjU27bFqK

  • 1
    Thanks Luis, I had to increase the number of allowed characters in the input field as well, from 12 to 16, but it's working great now! – Joseph McCarthy May 21 '15 at 15:55
0

Threw together a quick custom filter and dropped the ngCurrency library.

<input type="number" maxlength="12" ng-currency ng-model="myValue" />
{{myValue | myCurrency:myValue | currency:'$':0}}

currencyModule.filter('myCurrency', function () {
    return function (myValue) {
        if (myValue > 999999){
          myValue = 999999;
        }
        else if(myValue < 0){
          myValue = 0;
        }
        return myValue;
    };
});

http://plnkr.co/edit/1nw9bW3BTkU5YRa2lJFg?p=preview

adamjld
  • 310
  • 1
  • 9