Last active
February 22, 2017 11:28
-
-
Save Avinashachu007/618e8288dec1106571a44f46e2f46b3d to your computer and use it in GitHub Desktop.
A library to handle Currencies in JavaScript. By default, It will handle Indian Currency. Change 'Currency.prototype.locale' & 'Currency.prototype.currency' to Handle whatever currency of your wish
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
Number.prototype.toCurrency = function(){ // Prototype Added to Number property to convert Number into Currncy Object | |
return new Currency(this.valueOf()); | |
}; | |
function Currency(args){ //Important: Currency Object Constructor | |
this.value = args*Currency.prototype.multiple; | |
return this.value; | |
} | |
Currency.prototype.multiple = 1000; // Currency is always in paisa. If u want accuracy of two digit after decimal set it 100; For three digit accuracy set into 1000 | |
Currency.prototype.locale = 'en-IN'; // Currency should be Displayed in Indian Currency format '1,10,00,000'(Crores,Lakhs,Thousands) | |
Currency.prototype.currency = 'INR'; // Symbol shown before the amount | |
Currency.prototype.valueOf = function(){ // Important: When we declare valueOf function the variable can be use alike primitive data type | |
return this.value; //Value to be returned when object.valueOf() is called | |
} | |
Currency.prototype.showCurrency = function(){ // Display the Number as Currency with Currency symbol | |
return (this.valueOf()/Currency.prototype.multiple).toLocaleString(Currency.prototype.Locale, { style: 'currency', currency: Currency.prototype.currency }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment