0% found this document useful (0 votes)
116 views

Code For Converting Number To Words

This document contains JavaScript functions to convert integers and currency amounts to words in the Indian numbering system. It includes functions to convert numbers to words using either the official or common numbering systems, and to convert currency amounts to word phrases. It tests the functions on various input values to validate the output.

Uploaded by

rohit singh
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
116 views

Code For Converting Number To Words

This document contains JavaScript functions to convert integers and currency amounts to words in the Indian numbering system. It includes functions to convert numbers to words using either the official or common numbering systems, and to convert currency amounts to word phrases. It tests the functions on various input values to validate the output.

Uploaded by

rohit singh
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

/*********************************************************************

* @function : integerToWordsInd()
* @purpose : Converts Unsigned Integers to Indian Numeral Words
* With options for either the Official or the
* Crore-Lakh Counting Systems
* @version : 1.00
* @author : Mohsen Alyafei
* @date : 07 July 2020
* @param : {number} [integer numeric or string]
* @param : Optional {boolean} [Official]
* 0 = Use Crore-Lakh Counting System (default)
* Non 0 = Use the Official System
* @returns : {string} The wordified number string
**********************************************************************/
var Table_0_19 =
["","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","
Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"
],
Table_20_90=
["","","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"],
Table_Scale=
["","Thousand","Lakh","Crore","Arab","Kharab","Neel","Padma","Shankh","Samudra","An
tya","Madhyam","Paraardh","***","***"];
//===================================================================
function integerToWordsInd(Num=0 , Official=0) {
if (Num===0) return "Zero";
if (Official) return Siptlets(Num); // Return Official Numbering
System text
let NumWords="";
Num = ("0".repeat(6*(Num+="").length % 7) +Num).match(/.{7}/g); // Create Siptlets
Array
return Num.forEach((Siptlet, ScalePos) => { // Return Commmon-Use
Numbering System text
let [Scale,SWords] = [(Table_Scale[3]+" ").repeat(Num.length-ScalePos-
1).trimRight(), Siptlets(Siptlet)];
NumWords +=(NumWords && SWords ? ", " : "") +SWords +(Scale ? " " : "") +Scale;
}), NumWords;
//===================================================================
function Siptlets(Num, NumWords="") { // Core function (Called for
both Systems)
(Num+="").length-3 & 1 && (Num="0"+Num);
Num = Num> 999 ? [...Num.slice(0,-3).match(/.{2}/g).map(e => "0"+e),(Num.slice(-
3))]:[("00"+Num).substr(-3)];
return Num.forEach((Duplet,ScalePos) => {if (+Duplet) {
let [Hyphen,Hundreds,Tens,Scale] = [+Duplet[2] ? "-" : "",+Duplet[0],
+Duplet.substr(1),Table_Scale[Num.length-ScalePos-1]];
NumWords += (NumWords ? " " : "") + (Hundreds ? Table_0_19[Hundreds] + "
Hundred" :"") +
(Hundreds && Tens ? " " : "") + (Tens< 20 ? Table_0_19[Tens] :
Table_20_90[+(Duplet[1])] + Hyphen + Table_0_19[+Duplet[2]]);
NumWords += (NumWords && Scale ? " " : "") + Scale;
}}), NumWords;}
}
//===================================================================

//===================================================================
// Extra Function if needed for Indian Currency
// Uses same input parameters as the above main function
//===================================================================
function numberCurrencyIn(Num=0 , Official=0) {
let n= (Num+"").split(0.1.toLocaleString().substr(1,1)); // Number and Fraction
parts
n.length!==2 && (n[1]= ""); // No fraction
Num= n[0];
let Nw="", Fw="", Frc = (n[1]+"00").substring(0,2); // Limit to 2 Decimal Places
Num && (Nw= integerToWordsInd(Num,Official)); // Convert the Whole Number
Frc && (Fw= integerToWordsInd(Frc,Official)); // Convert the Fractional Part
return (Nw ? Nw:"") + (Nw ? " Rupees":"") + (Nw && Fw ? " and ":"") + (Fw ? Fw+"
Paisa":""); // Join together
}
//===================================================================

//===================================================================
// Test Cases
//===================================================================
// 1. Test Numbers under Common-Use Numbering System
//===================================================================
var r=0; // test tracker
r |= testN(50,"Fifty");
r |= testN(12000,"Twelve Thousand");
r |= testN(777000,"Seven Lakh Seventy-Seven Thousand");
r |= testN(550001,"Five Lakh Fifty Thousand One");
r |= testN(12345678,"One Crore, Twenty-Three Lakh Forty-Five Thousand Six Hundred
Seventy-Eight");
r |= testN(123456789,"Twelve Crore, Thirty-Four Lakh Fifty-Six Thousand Seven
Hundred Eighty-Nine");
r |= testN(1234567890,"One Hundred Twenty-Three Crore, Forty-Five Lakh Sixty-Seven
Thousand Eight Hundred Ninety");
r |= testN(12345678900,"One Thousand Two Hundred Thirty-Four Crore, Fifty-Six Lakh
Seventy-Eight Thousand Nine Hundred");
if (r==0) console.log("Test Case 1 Numbers (Common-Use Numbering System) Passed.");
//===================================================================
// 2. Test Numbers under Official Numbering System
//===================================================================
var r=0; // test tracker
r |= testN(50,"Fifty");
r |= testN(12000,"Twelve Thousand",true);
r |= testN(777000,"Seven Lakh Seventy-Seven Thousand",true);
r |= testN(550001,"Five Lakh Fifty Thousand One",true);
r |= testN(12345678,"One Crore Twenty-Three Lakh Forty-Five Thousand Six Hundred
Seventy-Eight",true);
r |= testN(123456789,"Twelve Crore Thirty-Four Lakh Fifty-Six Thousand Seven
Hundred Eighty-Nine",true);
r |= testN(1234567890,"One Arab Twenty-Three Crore Forty-Five Lakh Sixty-Seven
Thousand Eight Hundred Ninety",true);
r |= testN(12345678900,"Twelve Arab Thirty-Four Crore Fifty-Six Lakh Seventy-Eight
Thousand Nine Hundred",true);
if (r==0) console.log("Test Case 2 Numbers (Official Numbering System) Passed.");
//===================================================================
// 3. Test Currency under Common-Use Numbering System
//===================================================================
var r=0; // test tracker
r |= testC(1,"One Rupees");
r |= testC(2.0,"Two Rupees");
r |= testC(2.01,"Two Rupees and One Paisa");
r |= testC(0.3,"Thirty Paisa");
r |= testC(.3,"Thirty Paisa");
r |= testC(3002900000.50,"Three Hundred Crore, Twenty-Nine Lakh Rupees and Fifty
Paisa");
r |= testC(220000,"Two Lakh Twenty Thousand Rupees");
if (r==0) console.log("Test Case 3 Currency (Common-Use Numbering System)
Passed.");
//===================================================================
// 4. Test Currency under Official Numbering System
//===================================================================
var r=0; // test tracker
r |= testC(3002900000.50,"Three Arab Twenty-Nine Lakh Rupees and Fifty
Paisa",true);
r |= testC(55000000000,"Fifty-Five Arab Rupees",true);
if (r==0) console.log("Test Case 4 Currency (Official Numbering System) Passed.");
//===================================================================
function testN(n,tobe,f) {let r = integerToWordsInd(n,f);
if (r !== tobe) {console.log(`${n} Output : ${r}\n${n} Should be: $
{tobe}`);return 1;}}
function testC(n,tobe,f) {let r = numberCurrencyIn(n,f);
if (r !== tobe) {console.log(`${n} Output : ${r}\n${n} Should be: $
{tobe}`);return 1;}}

You might also like