3
Good morning, everyone.
Next, I need to make when the user leaves with the focus of the field or when typing it format this field according to the format of money, first in the Brazilian format.
Example: 14700,25 or 14,00. Without the points, if not Angularjs can not add up.
However, when using the code below, the function toFixed(2) removes the zeroes from the decimals and, for example, if the user informs 32, it adds the two zeroes after the comma, but when modifying the model continues 32 and not 32.00. And if you do not put parseFloat when modifying the model, the field is left blank.
Follows the code:
function formatarNumero(id, campo){
var scope = angular.element(document.getElementById('divVendaProdutos')).scope();
var valor_campo;
if (campo == 1){
valor_campo = $('#quantidade' + id)[0].value;
var amt = parseFloat(valor_campo);
amt = amt.toFixed(2);
scope.$apply(function () {
scope.invoice.items[id].quantidade = parseFloat(amt);
});
}
if (campo == 2){
valor_campo = $('#valorUnitario' + id)[0].value;
var amt = parseFloat(valor_campo);
amt = amt.toFixed(2);
scope.$apply(function () {
scope.invoice.items[id].valorUnitario = parseFloat(amt);
});
}
}
What is the best way to solve this problem?
Thank you in advance.
What’s your controller like? I think you should be formatting the value inside, with an Angular filter.
– bfavaretto
Have you tried using the Angularjs currency filter?
– adelmo00