Javascript / html (Writing the Word Equivalent of a Check Amount) Continuing the
ID: 3598722 • Letter: J
Question
Javascript / html
(Writing the Word Equivalent of a Check Amount) Continuing the discussion in the preceding
exercise, we reiterate the importance of designing check-writing systems to prevent alteration
of check amounts. One common security method requires that the check amount be both written
in numbers and spelled out in words. Even if someone is able to alter the numerical amount of the
check, it’s extremely difficult to change the amount in words.
Many computerized check-writing systems do not print the amount of the check in words.
Perhaps the main reason for this omission is that most high-level languages used in commercial
applications do not contain adequate string-manipulation features. Another reason is that the logic
for writing word equivalents of check amounts is somewhat involved.
Write a script that inputs a numeric check amount and writes the word equivalent of the
amount. For example, the amount 112.43 should be written as
ONE HUNDRED TWELVE and 43/100
Explanation / Answer
In this answer , I developed a method getWords() to convert numbers from 0 to 99999999999 (ninety nine billion ninety nine crore ninety nine lakh ninety nine thousand nine hundred and ninety nine) into corresponding words. I have followed a recursive approach. Firstly, the input number is splitted into integer part and float part. The integer part’s length is calculated (number of digits). Based on the number of digits, corresponding functions are defined; which include getTens() , getHundreds(), getThousands() … getBillions(). These functions are recursive in nature. That is ; the method getBillions() will receive an input number which is in billions calls the getCrores() method and passes the crores part; the getCrores() method receives a number in crores and calls getLakhs() and pass the lakhs part; and just like that, all the other methods are defined. Using this method getWords(); you can convert a number upto 11 digits into words. More info is available in comments. Please forgive if the indentation appear to be bad. I’m having a hard time to maintain it when I copy it from my development software to here. Thanks.
(Please note one more thing that I’ve used Asian numbering system ( hundreds, thousands, lakhs, crores, billions)
//HTML CODE WITH JAVASCRIPT
<!DOCTYPE html>
<html>
<title>Web Page Design</title>
<body>
<script>
var str='';
/**
* The units variable stores numbers from one to nineteen
* */
var units = ['','ONE ','TWO ','THREE ','FOUR ', 'FIVE ','SIX ','SEVEN ','EIGHT ','NINE ','TEN ','ELEVEN ','TWELVE ','THIRTEEN ','FOURTEEN ','FIFTEEN ','SIXTEEN ','SEVENTEEN ','EIGHTEEN ','NINETEEN '];
/**
* The tens variable stores tens numbers
*/
var tens = ['', '', 'TWENTY','THIRTY','FORTY','FIFTY', 'SIXTY','SEVENTY','EIGHTY','NINETY'];
function getWords(num) {
/**
* This method will accept a number and prints the
* number in words
* */
num=Number(num);
var int_part=Math.floor(num); /*to strip the integer part*/
var float_part=(num%1).toFixed(2); /*to strip the float part*/
var length=int_part.toString().length; /*length of the integer(how many digits)*/
var hitMaxim=false; /*a flag to check if the limits exceeded*/
document.write("entered number: "+num+", int: "+int_part+", float: "+float_part+", len: "+length); /*printing the original number*/
switch(length){
/*
*switching based on the number of digits
*/
case 0:str='';
break;
case 1:str=units[int_part];
break;
case 2:str=getTens(int_part);
break;
case 3:str=getHundreds(int_part);
break;
case 4:str=getThousands(int_part);
break;
case 5:str=getThousands(int_part);
break;
case 6:str=getLakhs(int_part);
break;
case 7:str=getLakhs(int_part);
break;
case 8:str=getCrores(int_part);
break;
case 9:str=getCrores(int_part);
break;
case 10:str=getBillions(int_part);
break;
case 11:str=getBillions(int_part);
break;
default: str+='maximum reached';
hitMaxim=true;
break;
}
if(!hitMaxim){
if(float_part!=0.0)
{
str=str+' AND '+(float_part*100)+'/100';
}
}
document.write("<br>number in words: "+str);
}
function getTens(n){
/*
* method to convert tens into words
*/
var x='';
if(n<20){
x=units[n];
}else{
var t=parseInt(n/10);
x=tens[t];
var u=n%10;
x+=' '+units[u];
}
return x;
}
function getHundreds(n){
/*
* method to convert hundreds into words
*/
var x='';
var t=parseInt(n/100);
if(t==0){
return getTens(n%100);
}
x+=units[t]+' HUNDRED';
x+=' '+getTens(n%100);
return x;
}
function getThousands(n){
/*
* method to thousands into words
*/
var x='';
var t=parseInt(n/1000);
if(t==0){
return getHundreds(n%1000);
}
if(t<20){
x+=units[t]+' THOUSAND';
}else{
x+=getTens(t)+ ' THOUSAND';
}
x+=' '+getHundreds(n%1000);
return x;
}
function getLakhs(n){
/*
* method to lakhs into words
*/
var x='';
var t=parseInt(n/100000);
if(t==0){
return getThousands(n%100000);
}
if(t<20){
x+=units[t]+' LAKH';
}else{
x+=getTens(t)+ ' LAKH';
}
x+=' '+getThousands(n%100000);
return x;
}
function getCrores(n){
/*
* method to convert crores into words
*/
var x='';
var t=parseInt(n/10000000);
if(t==0){
return getLakhs(n%10000000);
}
if(t<20){
x+=units[t]+' CRORE';
}else{
x+=getTens(t)+ ' CRORE';
}
x+=' '+getLakhs(n%10000000);
return x;
}
function getBillions(n){
/*
* method to convert billions into words
*/
var x='';
var t=parseInt(n/1000000000);
if(t==0){
return getCrores(n%1000000000);
}
if(t<20){
x+=units[t]+' BILLION';
}else{
x+=getTens(t)+ ' BILLION';
}
x+=' '+getCrores(n%1000000000);
return x;
}
getWords(11810100.50); /*calling the function to convert the number into words*/
</script>
</body>
</html>
//OUTPUT
entered number: 11810100.5, int: 11810100, float: 0.50, len: 8
number in words: ONE CRORE EIGHTEEN LAKH TEN THOUSAND ONE HUNDRED AND 50/100
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.