Last active
April 26, 2016 14:00
-
-
Save victorvaz/80346a2cba06523d1a18743b7feebc9b to your computer and use it in GitHub Desktop.
Função para formatar um número com Javascript
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
/** | |
* Função para formatar um número colocando pontos. | |
* @author Víctor Vaz <victor-vaz@hotmail.com> | |
* @param n Número para ser formatado. | |
* @returns {string} Número formatado. | |
*/ | |
function formatarNumero(n) { | |
var n = n.toString(); | |
var r = ''; | |
var x = 0; | |
for (var i = n.length; i > 0; i--) { | |
r += n.substr(i - 1, 1) + (x == 2 && i != 1 ? '.' : ''); | |
x = x == 2 ? 0 : x + 1; | |
} | |
return r.split('').reverse().join(''); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment