Last active
June 4, 2018 18:40
-
-
Save HugoRoca/02a5e04b8c59bbd712c637969fa338e9 to your computer and use it in GitHub Desktop.
Función para convertir un numero a 2 decimales
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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