Created
February 19, 2013 20:48
-
-
Save felipemorais/4989763 to your computer and use it in GitHub Desktop.
Format Currency - BRL
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
function formatCurrency( value ) { | |
var exit = []; | |
var i; | |
value = parseFloat( value ); | |
value = value.toString().split("").reverse(); | |
for( i = 0; i < value.length; i++ ) { | |
if( i == 2 ) { | |
exit.push( "," ); | |
} else if ( ( i - 2 ) % 3 == 0 ) { | |
exit.push( "." ); | |
} | |
exit.push( value[ i ] ); | |
}; | |
if(exit[0] == "."){ | |
exit.shift(); | |
} | |
return exit.reverse().join(""); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment