Add numbers respecting formatting R $ 00,00

Asked

Viewed 82 times

3

Good afternoon

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

R$ 00,00

But I’m having trouble with decimal numbers,

instead of the number staying R$ 15,10 it gets in formatting R$ 15,1

Can you help me?

Below is the progress of 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>

3 answers

3


Hello Perhaps this passage will help you:

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

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>

2

Just you call the toFixed(2) at the exhibition.

This way your calculation is made independent of the decimals and displayed in a rounded form with as many houses as you want.

1

I included one more instruction: total = total.toFixed(2);

See more rises toFixed here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed

See how it looks:

<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>

Browser other questions tagged

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