Skip to content

Instantly share code, notes, and snippets.

@HugoRoca HugoRoca/number2decimal
Last active Jun 4, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.