Javascript money format with Angularjs and jQuery

Asked

Viewed 18,668 times

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.

  • Have you tried using the Angularjs currency filter?

4 answers

7

6


Use the currency in accordance with that documentation.

You can also specify which symbol separates decimal and integer numbers and how many decimal places you want

EDIT: This filter is only to display on the screen and/or format after submitting the form.

Would look like this: <input ng-model="campoDinheiro" value="{{campoDinheiro | currency:'R$'}}"/>

If you want to format in the Angularjs controller, it would look like this:

$filter('currency')(campoDinheiro, 'R$')



But, if you need to format while user is typing, create a Directive to do this. Take this example as the basis, but change prefix,centsSeparator,thousandsSeparator as you wish

  • Peter, could you explain how to use in the answer itself?

  • @bfavaretto Look if it helps

  • Great Peter, thank you

  • Thank you @Pedrolaini, I will test here in the project with the filter and not the directive, as it is for when the user takes the focus of the element in which he typed.

  • @Pedrolaini, the directive worked, but the calculation that multiplied the price by the amount of items stopped working. And in the example you gave me I tried to do so {{ test * test |json }} to see if it worked, but returned null, how can I do the multiplication?

  • you need to do the calculation in the controller and assign it to the variable... this notation just takes the variable and applies the filter, can not do calculation inside it

  • 1

    Thanks, I figured out how to fix this, only I had to change the input type to number and when the user type the number is quickly removed. What I did was just take the . (points) so it can be calculated outside the controller.

Show 2 more comments

2

Great day, you can also use the angular language pack, on the website of the angular itself you find, and when showing some field that needs formatting, as dates or monetary values, angular already does the job of placing siphon or formatting for the configuration used in the chosen allotment.

This Uto brings a step-by-step that can help a lot, how to use angular locale, hello world.

In case to download locales supported by Angularjs this link to the Github project displayed the entire list.

2

Ideally you should not apply the format in the model, but rather in the view, ie in HTML code.

To apply value formatting Angularjs offers the concept of filter, which you indicate in HTML along with the value to be formatted.

For example, to format a text box in monetary format you can use the following code on the HTML page:

<input type="text" value="{{valorUnitario | currency}}" />

Optionally you can also inform the currency symbol that should be used by formatting:

<input type="text" value="{{valorUnitario | currency:'R$'}}" />

This way the box and text will always be formatted, still keeping the model value without formatting, so that it will not disturb in any way the calculations with this value.

  • I tested this way, but when typing is not putting the dots and commas when taking the Focus input.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.