How to format money with jade in Node.js?

Asked

Viewed 146 times

3

I have to present monetary values in my view, but it is appearing without formatting. For example, 5.4237 should be presented as R$ 5,42, but I don’t know how to format this value using the Jade with Node.js.

I need the value to be saved in the system as a pure number, without formatting, as this number is used in calculations that would lose accuracy if saved only with two decimal places. However, to show the value on the screen I do not need the other decimals, so I believe that the best way is to format the value when showing it to the user.

How do I format these monetary values in jade?

1 answer

2


There are several packages on npm that can serve what you need:

Jade itself does not have a special structure for conversion into monetary format.

Example:

// Adiciona o middleware accounting ao contexto da view.
data["accounting"] = require('accounting');

<!-- Chama o objeto accounting para formatar o valor monetário. -->
p Total = #{accounting.formatMoney(5.4237, "R$ ", 2, ",", ".")}

Result obtained by: Total = R$ 5,42

  • It is. I’m adding Accounting to the template context and calling the formatting method inside the view, but I wanted a more "native" way to do this. If you don’t have it, you can use it anyway.

  • It is. It doesn’t. Jade is more an HTML template than a complete graphic suite.

  • It worked. Thanks for the reply.

Browser other questions tagged

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