Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for number to words conversion #123

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 137 additions & 1 deletion accounting.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@
decimal : ".", // decimal point separator
thousand : ",", // thousands separator
precision : 2, // decimal places
grouping : 3 // digit grouping (not implemented yet)
grouping : 3, // digit grouping (not implemented yet)

//convert numbers to words
majorCurrency: "Dollars",
minorCurrency: "Cents",
numberingSystem: "Arabic" // only 'Arabic' and 'Indian' Supported
},
number: {
precision : 0, // default precision on numbers is 0
Expand Down Expand Up @@ -375,6 +380,137 @@
});
};

/* ----------- number to words --------- */
var digitWords = ['Zero ', 'One ', 'Two ', 'Three ', 'Four ', 'Five ', 'Six ', 'Seven ', 'Eight ', 'Nine '];
var teenWords = ['', 'Eleven ', 'Twelve ', 'Thirteen ', 'Fourteen ', 'Fifteen ', 'Sixteen ', 'Seventeen ', 'Eighteen ', 'Nineteen '];
var tenerWords = ['', 'Ten ', 'Twenty ', 'Thirty ', 'Forty ', 'Fifty ', 'Sixty ', 'Seventy ', 'Eighty ', 'Ninety '];
NumberingSystems = {
'Arabic':['', 'Thousand ', 'Million ', 'Billion ','Trillion ', 'Quadrillion ', 'Quintillion '],
'Indian':['', 'Hundred ', 'Thousand ', 'Lakh ', 'Crore ', 'Arab ', 'Kharab ', 'Nil ', 'Padm ', 'Sankh ']
};

lib.toWords = function(number) {
number = unformat(number, lib.settings.number.decimal) //Get rid of leading and trailing zeroes
number = number.toString();

if (number != parseFloat(number))
return 'Not a Number';

var decimalIndex = number.indexOf('.');
if (decimalIndex == -1)
decimalIndex = number.length;

if (decimalIndex > 18)
return 'Number is too large. Can not process.';

switch(lib.settings.currency.numberingSystem) {
case 'Arabic':
return toArabicWordRepresentation(number, decimalIndex)
break;
case 'Indian':
return toIndianWordRepresentation(number, decimalIndex)
break;
default:
return 'NumberingSystem ' + lib.settings.currency.numberingSystem + ' not supported.';
};
};

var toArabicWordRepresentation = function(number, decimalIndex){
digits = number.split('');
var stringRepresentation = "";
systemIndex = 0
for (var i = decimalIndex - 1; i >= 0; i--) {
if (digits[i] == 0){
if(Number(digits[i-1]) !== 0 && Number(digits[i-2]) !== 0){
stringRepresentation = NumberingSystems.Arabic[systemIndex] + stringRepresentation
}
}
else{
if (digits[i-1] == 1) {
stringRepresentation = NumberingSystems.Arabic[systemIndex] + stringRepresentation
}
else{
stringRepresentation = digitWords[digits[i]] + NumberingSystems.Arabic[systemIndex] + stringRepresentation;
}
}

i--;
if (digits[i] !== undefined)
if (digits[i] == 1)
stringRepresentation = teenWords[digits[i+1]] + stringRepresentation;
else
stringRepresentation = tenerWords[digits[i]] + stringRepresentation;

i--;
if (digits[i] != undefined)
if (digits[i] != 0)
stringRepresentation = digitWords[digits[i]] + 'Hundred ' + stringRepresentation;
systemIndex++;
}

return toWordHelper(stringRepresentation, digits, decimalIndex)
}

var toIndianWordRepresentation = function(number, decimalIndex){
digits = number.split('');
var stringRepresentation = "";
systemIndex = 0;

for (var i = decimalIndex - 1; i >= 0; i--) {
if (systemIndex == 1){
if (digits[i] != 0)
stringRepresentation = digitWords[digits[i]] + NumberingSystems.Indian[systemIndex] + stringRepresentation
systemIndex++
continue
}

if (digits[i] == 0){
if(Number(digits[i-1]) !== 0 && Number(digits[i-2]) !== 0){
stringRepresentation = NumberingSystems.Indian[systemIndex] + stringRepresentation
}
}
else{
if (digits[i-1] == 1) {
stringRepresentation = NumberingSystems.Indian[systemIndex] + stringRepresentation
}
else{
stringRepresentation = digitWords[digits[i]] + NumberingSystems.Indian[systemIndex] + stringRepresentation;
}
}

i--;
if (digits[i] != undefined)
if (digits[i] == 1)
stringRepresentation = teenWords[digits[i+1]] + stringRepresentation;
else
stringRepresentation = tenerWords[digits[i]] + stringRepresentation;

systemIndex++;
}

return toWordHelper(stringRepresentation, digits, decimalIndex)
}

var toWordHelper = function(stringRepresentation, digits, decimalIndex){

if (stringRepresentation === "")
stringRepresentation = "Zero ";

stringRepresentation = stringRepresentation + lib.settings.currency.majorCurrency

if (decimalIndex != digits.length) {
stringRepresentation += ' and ';

for (var i = decimalIndex + 1; i < digits.length; i++)
stringRepresentation += digitWords[digits[i]];

stringRepresentation += lib.settings.currency.minorCurrency;
}
return stringRepresentation + ' Only';
}

/* ------------ End number to words -------------- */


/* --- Module Definition --- */

Expand Down
20 changes: 20 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,26 @@ <h4><strong>accounting.unformat()</strong></h4>
// which part of the number is a decimal/float:
accounting.unformat("&euro; 1.000.000,00", ","); // 1000000</pre>


<h4><strong>accounting.toWords()</strong></h4>

<pre class="prettyprint lang-js">// Standard usage and parameters (returns String):
accounting.toWords(string);
accounting.toWords(number);

//Check default settings in settings.currency
// Example usage:
accounting.toWords("1234567") //"One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven Dollars Only"
accounting.toWords(1234567.35) //"One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven Dollars and Three Five Cents Only"

accounting.settings.currency.majorCurrency = "Rupees";
accounting.settings.currency.minorCurrency = "Paise";
accounting.settings.currency.numberingSystem = 'Indian';
accounting.toWords("1234567") //"Twelve Lakh Thirty Four Thousand Five Hundred Sixty Seven Rupees Only"
accounting.toWords(1234567.35) //Twelve Lakh Thirty Four Thousand Five Hundred Sixty Seven Rupees and Three Five Paise Only"
</pre>


</section>


Expand Down
21 changes: 21 additions & 0 deletions tests/qunit/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,26 @@ $(document).ready(function() {


});

test("accounting.toWords()", function(){
equals(accounting.toWords("1234567"), "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven Dollars Only", "Converts numbers to words with default settings.");
equals(accounting.toWords("1234567.35"), "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven Dollars and Three Five Cents Only", "Converts numbers to words with default settings.");

//user defined settings
accounting.settings.currency.majorCurrency = "Rupees";
accounting.settings.currency.minorCurrency = "Paise";
accounting.settings.currency.numberingSystem = 'Indian';
equals(accounting.toWords("1234567"), "Twelve Lakh Thirty Four Thousand Five Hundred Sixty Seven Rupees Only", "Converts numbers to words with custom settings.");
equals(accounting.toWords("1234567.35"), "Twelve Lakh Thirty Four Thousand Five Hundred Sixty Seven Rupees and Three Five Paise Only", "Converts numbers to words with custom settings.");
accounting.settings.currency.majorCurrency = "Dollars";
accounting.settings.currency.minorCurrency = "Cents";
accounting.settings.currency.numberingSystem = 'Arabic';

accounting.settings.number.decimal = ','
equals(accounting.toWords("1234567,35"), "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven Dollars and Three Five Cents Only", "Converts numbers to words with default settings, picks decimal from settings.");
equals(accounting.toWords("123,4567,35"), "One Hundred Twenty Three Dollars and Four Five Six Seven Cents Only", "Converts numbers to words with default settings, picks decimal from settings.");
accounting.settings.number.decimal = '.'

});

});