I need help using the bigmoney.js plug.

Asked

Viewed 66 times

0

Hello, I need to do calculations like the bigmoney.js plug, someone explain me with examples how to use.. make transactions in our "real" currency, need mainly for percentage, how to calculate percentage and and dpois add this value to the result.

  • Have you read the documentation for this plugin ? It seems to me quite clear. What difficulties did you encounter?

  • I read the documentation, but I confess that I remain confused, I wanted to calculate 10% of a certain value and then add, I did it in pure javascript, but I did not feel confident, I was giving many failures, searching I saw right here on the site someone talking about this plug, but I got lost in the time to make that kind of calculation.

  • I really appreciate it if you just give me a small example in our currency, the real.

1 answer

0

Include the plugin

First you have to start by including the plugin and particularly the file bigmoney-all.js:

<script src="bigmoney-all.js"></script>

If you are working with Node JS you can do so at the expense of require('bigmoney'), as long as you put it in the folder beforehand node_modules.

Configure the conversions

Then you can set up the various currencies available as well as your conversion rates if you wanted to convert between currencies:

Money.settings = {
    base: "BRL",
    rates: {
        BRL : 1, //Esta é a base, as outras são calculadas convertendo a partir desta
        USD: 0.31,
        EUR: 0.25
    },
    format: "%decimal %currency"
}

With special attention that the conversions I put forward refer to the day this reply was published.

Conversions are done by calling the method convert passing the currency to which you wish to convert:

dinheiro.convert('USD'); //obter o resultado da conversão para dólar americano

Create money

Now every time you want to work with the currency just create a new object with Money() and specify the currency:

let dinheiro = Money(325, "BRL");

Available operations

If you want to operate on this money you can use the various arithmetic operators provided by the library:

  • plus - add up
  • minus - subtract
  • times - multiply
  • div - divide
  • mod - rest of the division

Remembering that each operation returns the new result.

Now let’s look at an example where we calculate and add 10% of R$ 325:

let dinheiro = Money(325, "BRL");
dinheiro = dinheiro.plus(dinheiro.div(10)); //dinheiro soma com dinheiro a dividir por 10
console.log(dinheiro.valueOf()) //357.5

At any time want to know the value that the variable has just use the method valueOf.

Formatting

To show specifying a formatting you have to use the method format:

console.log(dinheiro.format("R$ %decimal")); //mostra R$ 357.5

You can even preset the currency format to simplify later formatting at the expense of formatter:

Money.formatter = function(decimal, currency, formatParam) {
    switch(currency) {
        case 'USD': return "$" + decimal;
        case 'EUR': return "€" + decimal;
        case 'JPY': return "¥" + decimal;
        case 'BRL': return "R$ " + decimal;
        default: return decimal + " " + currency;
    }
};

Now if you have money in real format, just show by calling format parameter-less:

let dinheiro = Money(100, "BRL");
console.log(dinheiro.format()); //mostra R$ 100

If you want you can even use the format available in Number.toLocaleString to simplify and get closer to the usual daily use:

Money.formatter = function(decimal, currency, formatParam) {
    switch(currency) {
        case 'USD': return "$" + decimal;
        case 'EUR': return "€" + decimal;
        case 'JPY': return "¥" + decimal;
        case 'BRL': return decimal.toLocaleString('pt-br',{style: 'currency', currency: 'BRL'});
        default: return decimal + " " + currency;
    }
};

let dinheiro = Money(100, "BRL");
console.log(dinheiro.format()); //mostra R$100,00
  • Our guy, it was really worth, the first part of instantiating the plug and stuff, I did it , but from now on the part of using the sum, dividing, it wasn’t working.. But now the way you explained it, I could understand how this part works. Thank you very much.

Browser other questions tagged

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