Add numbers respecting formatting R $ 00,00

3

Good afternoon

I'm trying to represent a sum that the output value is in the formatting below:

  

$ 00.00

But I'm having difficulty with decimal place numbers,

Instead of the number being $ 15.10 it gets in the format $ 15,1

Can you help me?

Here is the code:

<html>
<head>
<script type="text/javascript">
function id( el ){
        return document.getElementById( el );
}
function getMoney( el ){
        var money = id( el ).value.replace(/[^0-9]/g,'');
        return parseFloat( money );
}
function soma()
{
        var total = (getMoney('campo1')+getMoney('campo2')+getMoney('campo3'))/100;

                    id('campo4').value = ('R$'+total).toString().replace('.', ',');

}
</script>
</head>
<body>
        <form action="" method="">
                <input name="campo1" id="campo1" value="10,00" /><br />
                <input name="campo2" id="campo2" value="4,10" /><br />
                <input name="campo3" id="campo3" value="1,00" /><br />
                <input name="campo4" readonly="readonly" id="campo4" /><br />
                <input type="button" onclick="soma()" value="Soma de Valores" />
        </form>
</body>
</html>
    
asked by anonymous 11.01.2017 / 14:25

3 answers

3

Hello Maybe this passage will help you:

parseFloat(10.4).toFixed("2").replace(".", ",").replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1.")

It would look like this:

<html>
<head>
<script type="text/javascript">
function id( el ){
        return document.getElementById( el );
}
function getMoney( el ){
        var money = id( el ).value.replace(/[^0-9]/g,'');
        return parseFloat( money );
}
function soma()
{
        var total = (getMoney('campo1')+getMoney('campo2')+getMoney('campo3'))/100;

                    id('campo4').value = ('R$'+parseFloat(total).toFixed("2").replace(".", ",").replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1."));

}
</script>
</head>
<body>
        <form action="" method="">
                <input name="campo1" id="campo1" value="10,00" /><br />
                <input name="campo2" id="campo2" value="4,10" /><br />
                <input name="campo3" id="campo3" value="1,00" /><br />
                <input name="campo4" readonly="readonly" id="campo4" /><br />
                <input type="button" onclick="soma()" value="Soma de Valores" />
        </form>
</body>
</html>
    
11.01.2017 / 14:40
2

Just call toFixed (2) in the display.

This way your calculation is done independently of the decimal places and displayed in a rounded way with as many houses as you wish.

    
11.01.2017 / 15:05
1

I've included one more statement: total = total.toFixed(2);

See more sobe toFixed here: link

Here's how:

<html>
<head>
<script type="text/javascript">
function id( el ){
        return document.getElementById( el );
}
function getMoney( el ){
        var money = id( el ).value.replace(/[^0-9]/g,'');
        return parseFloat( money );
}
function soma()
{
        var total = (getMoney('campo1')+getMoney('campo2')+getMoney('campo3'))/100;
        total = total.toFixed(2);

                    id('campo4').value = ('R$'+total).toString().replace('.', ',');

}
</script>
</head>
<body>
        <form action="" method="">
                <input name="campo1" id="campo1" value="10,00" /><br />
                <input name="campo2" id="campo2" value="4,10" /><br />
                <input name="campo3" id="campo3" value="1,00" /><br />
                <input name="campo4" readonly="readonly" id="campo4" /><br />
                <input type="button" onclick="soma()" value="Soma de Valores" />
        </form>
</body>
</html>
    
11.01.2017 / 14:47