How to format money with jade in node.js?

3

I have to display monetary values in my view, but it is appearing without formatting. For example, 5.4237 should be displayed as R$ 5,42 , but I do not know how to format this value using Jade with node.js .

I need the value to be saved in the system as a pure, unformatted number because that number is used in calculations that would lose accuracy if saved only with two decimal places. However, to display the value on the screen I do not need the other decimal places, so I think the best way is to format the value when it is shown to the user.

How do I format these monetary values in jade?

    
asked by anonymous 05.11.2015 / 13:48

1 answer

2

It has several packages in npm that can serve you 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:     Total = $ 5.42

    
05.11.2015 / 14:23