Tengo el siguiente código JavaScript:
var calculation = $('select[name=somevalue1] option:selected').data('calc')-($('select[name=somevalue1] option:selected').data('calc')/100*$('select[name=somevalue2] option:selected').data('calc'))-($('select[name=somevalue1] option:selected').data('calc')-($('select[name=somevalue1] option:selected').data('calc')/100*$('select[name=somevalue2] option:selected').data('calc')))/100*$('select[name=somevalue3] option:selected').data('calc'); $('#calculation').text((calculation).toFixed(2).replace(".",",")) Eso calculará un número, cambio . a , y redondearlo a dos dígitos después de la coma.
Lo que debo hacer es agregar un punto después de los dígitos del árbol antes de venir.
Medio:
1.234,56 en lugar de 1234,56
1.234.567,89 en lugar de 1234567,89
¿Alguien sabe una manera de hacer eso?
Este código funcionó para mí:
<p id="calculation"></p> <script> function numberWithCommas(x) { var parts = x.toString().split("."); parts[0]=parts[0].replace(/\B(?=(\d{3})+(?!\d))/g,"."); return parts.join(","); } var calculation = 1231739216.34367; document.getElementById("calculation").innerHTML = numberWithCommas((calculation).toFixed(2)) </script>Este código:
ECMAScript Internationalization API tiene una función de formateo de números. Intl.NumberFormat()
Puedes usarlo así con javascript puro:
var calculation = document.getElementById('money').value; //1st way var moneyFormatter = new Intl.NumberFormat(); document.getElementById('formattedMoney').innerText = moneyFormatter.format(calculation); // 2nd way, using currency var moneyFormatter2 = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', minimumFractionDigits: 2 }); document.getElementById('formattedMoney2').innerText = moneyFormatter2.format(calculation); <label>Calculation:</label> <input type="text" value="123456789.25" name="money" id="money" /> <ul> <li>Number Format: <b><span id="formattedMoney"></span></b><br/><br/></li> <li>Currency Format: <b><span id="formattedMoney2"></span><b/></li> </ul>IE11, Firefox y Chrome lo admiten, pero no estoy seguro acerca de otros navegadores.
http://www.ecma-international.org/ecma-402/1.0/#sec-11.1.2
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat
Debe usar un String#replace adicional con una expresión regular (para hacer coincidir los dígitos en grupos de tres antes de , ) y una función de reemplazo (para anteponer . a cada coincidencia).
var calculation = $('select[name=somevalue1] option:selected').data('calc') - ($('select[name=somevalue1] option:selected').data('calc') / 100 * $('select[name=somevalue2] option:selected').data('calc')) - ($('select[name=somevalue1] option:selected').data('calc') - ($('select[name=somevalue1] option:selected').data('calc') / 100 * $('select[name=somevalue2] option:selected').data('calc'))) / 100 * $('select[name=somevalue3] option:selected').data('calc') function format(n) { return n.toFixed(2).replace('.', ',').replace(/\d{3}(?=(\d{3})*,)/g, function(s) { return '.' + s }) } $('#calculation').text(format(calculation)) var calculations = [ 1234.56, 1234567.89, 1234, .12 ] function format (n) { return n.toFixed(2).replace('.', ',').replace(/\d{3}(?=(\d{3})*,)/g, function (s) { return '.' + s }) } console.log(calculations.map(format))