Created
December 6, 2013 13:29
-
-
Save dayvsonlima/7823817 to your computer and use it in GitHub Desktop.
number_to_currency em javascript
This file contains hidden or 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
Helper = { | |
config: { | |
locale: "pt-BR" | |
}, | |
init: function(){ | |
}, | |
number_to_currency: function(num) { | |
if(Helper.config.locale == "pt-BR"){ | |
var p = num.toFixed(2).split("."); | |
return "R$ " + p[0].split("").reverse().reduce(function(acc, num, i, orig) { | |
return num + (i && !(i % 3) ? "." : "") + acc; | |
}, "") + "," + p[1]; | |
} | |
if(Helper.config.locale == "en"){ | |
var p = num.toFixed(2).split("."); | |
return "$ " + p[0].split("").reverse().reduce(function(acc, num, i, orig) { | |
return num + (i && !(i % 3) ? "," : "") + acc; | |
}, "") + "." + p[1]; | |
} | |
} | |
} | |
window.Helper = Helper | |
$(document).on('ready page:load', function(){ | |
window.Helper.init() | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment