Replace point by comma in Javascript

4

Good evening

In the example below add the values of 3 text boxes, and the result comes out in the fourth, how to make this result appear with a comma and not a dot?

<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');
        id('campo4').value = 'R$ '+total/100;
}
</script>
</head>
<body>
        <form action="" method="">
                <input name="campo1" id="campo1" value="25,60" /><br />
                <input name="campo2" id="campo2" value="5,15" /><br />
                <input name="campo3" id="campo3" value="2,63" /><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.08.2016 / 23:10

1 answer

5

With toLocaleString() of JavaScript you can have this is tailored depending on the locale:

22.33.toLocaleString(); /* depende do locale */
"22,33"

Or use a predefined one, for example:

22.33.toLocaleString('EN');
"22.33"
22.33.toLocaleString('PT');
"22,33"
    
11.08.2016 / 23:59