Skip to content

Instantly share code, notes, and snippets.

@HugoRoca
Last active June 4, 2018 18:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HugoRoca/02a5e04b8c59bbd712c637969fa338e9 to your computer and use it in GitHub Desktop.
Save HugoRoca/02a5e04b8c59bbd712c637969fa338e9 to your computer and use it in GitHub Desktop.
Función para convertir un numero a 2 decimales
// validación para poner a decimales
var formatNumber = {
separador: ",",
sepDecimal: '.',
formatear: function (num) {
num += '';
var splitStr = num.split('.');
var splitLeft = splitStr[0];
var splitRight = splitStr.length > 1 ? this.sepDecimal + splitStr[1] : '';
var regx = /(\d+)(\d{3})/;
while (regx.test(splitLeft)) {
splitLeft = splitLeft.replace(regx, '$1' + this.separador + '$2');
}
var res = splitLeft + splitRight;
var splitPrueba = res.split(".");
if (splitPrueba.length < 2) res = splitLeft + splitRight + ".00"
return res;
},
new: function (num, simbol) {
this.simbol = simbol || '';
return this.formatear(num);
}
}
function formatoMiles(valor) {
return formatNumber.new(valor);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment