angular-input-masks icon indicating copy to clipboard operation
angular-input-masks copied to clipboard

bug(money-mask): wrong number formating when value and decimals are setted asychronously

Open marcomafessolli opened this issue 9 years ago • 3 comments

Hi there.

I'm facing a problem with the money-mask. When the value and decimals are coming from a response from the server the money-mask formats the value wrong.

Codepen: http://codepen.io/marcomafessolli/pen/akEEYR

Looking into the code, it happens here: clearDelimitersAndLeadingZeros((parseFloat(value)).toFixed(decimals));, specifically here: (parseFloat(value)).toFixed(decimals). Because of that, a number with 3 decimals, when first setted, goes into the parser formatter with 2 decimals and gets rounded, losing one of the decimals.

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/36056067-bug-money-mask-wrong-number-formating-when-value-and-decimals-are-setted-asychronously?utm_campaign=plugin&utm_content=tracker%2F1022469&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F1022469&utm_medium=issues&utm_source=github).

marcomafessolli avatar Jul 15 '16 12:07 marcomafessolli

The problem seems to be the order of how the values are applied. The decimal value need to be applied first or you will loose precision.

Change your code to:

$timeout(function () {
    $scope.decimals2 = 3;
    $timeout(function () {
        $scope.moneyDecimals2 = 4.835;
    });
});

and I think your problem will be resolved.

assisrafael avatar Jul 23 '16 11:07 assisrafael

Hmmm, I see. I think there is no other way, yeah? In my scenario, decimals and moneyDecimals are coming from a http request, take a look;

Controller:

$http.get(/products/1)
    .then(function (response) {
        $scope.model = response.data
    });

View:

<input ng-model="model.moneyDecimal" ui-money-mask="model.decimals"/>

In this case there's nothing to do, correct?!

marcomafessolli avatar Jul 25 '16 00:07 marcomafessolli

Unfortunately yes, because the decimals setter is async.

assisrafael avatar Jul 25 '16 10:07 assisrafael