Skip to main content
New Participant
January 6, 2021
Answered

Convert numbers to word

  • January 6, 2021
  • 3 replies
  • 2789 views

I am trying to convert amount (numbers) into words.  I found a solution by googling with this code;

function ConvertToHundreds(num)
{
aTens = [ "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"];
aOnes = [ "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine",
"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen",
"Eighteen", "Nineteen" ];

var cNum, nNum;
var cWords = "";

num %= 1000;
if (num > 99) {
/* Hundreds. */
cNum = String(num);
nNum = Number(cNum.charAt(0));
cWords += aOnes[nNum] + " Hundred";
num %= 100;
if (num > 0)
cWords += " and "
}

if (num > 19) {
/* Tens. */
cNum = String(num);
nNum = Number(cNum.charAt(0));
cWords += aTens[nNum - 2];
num %= 10;
if (num > 0)
cWords += "-";
}

if (num > 0) {
/* Ones and teens. */
nNum = Math.floor(num);
cWords += aOnes[nNum];
}

return cWords;
}

function ConvertToWords(num)
{
var aUnits = [ "Thousand", "Million", "Billion", "Trillion", "Quadrillion" ];
var cWords = (num >= 1 && num < 2) ? "Dollar and " : "Dollars and ";
var nLeft = Math.floor(num);
for (var i = 0; nLeft > 0; i++) {
if (nLeft % 1000 > 0) {
if (i != 0)
cWords = ConvertToHundreds(nLeft) + " " + aUnits[i - 1] + " " + cWords;
else
cWords = ConvertToHundreds(nLeft) + " " + cWords;
}
nLeft = Math.floor(nLeft / 1000);
}
num = Math.round(num * 100) % 100;
if (num > 0)
cWords += ConvertToHundreds(num) + " Cents";
else
cWords += "Zero Cents";

return cWords;
}

 

The code is almost fine.  The issue is when the amount is 0 the result gives me Dollars and Zero Cents.

I need the result to be Zero.  Is there any chance we can correct this one.  Thank you very much.

This topic has been closed for replies.
Correct answer Nesa Nurani

Since you found that online il adapt to that code , not sure if you just want to show 'Zero' or add 'Zero' to other text but you can use like this:

var f = this.getField("Number");
if(f.value == 0)
event.value = "Zero";
else event.value = ConvertToWords(f.value);

 

 

3 replies

New Participant
January 28, 2022

(Somewhat late, but for the benefit of others:) Simply replace the line:

var cWords = (num >= 1 && num < 2) ? "Dollar and " : "Dollars and ";

with:

var cWords = (num >= 0 && num < 1) ? "Zero Dollars and " : (num >= 1 && num < 2) ? "Dollar and " : "Dollars and ";

New Participant
September 17, 2022

var cWords = (num >= 1 && num < 2) ? "Dollar and " : "Dollars and ";

with:

var cWords = (num >= 0 && num < 1) ? "Zero Dollars and " : (num >= 1 && num < 2) ? "Dollar and " : "Dollars and ";

 

I had replace the line but stil show like one Hundred Twenty Dollars and Zero Cent

I want One Hundred Twent Five Dollars 00/100

Thanks

New Participant
September 18, 2022

Replace this section at end of code:

 

num = Math.round(num * 100) % 100;
if (num > 0)
cWords += ConvertToHundreds(num) + " Cents";
else
cWords += "Zero Cents";

return cWords;

 

with

 

num = Math.round(num * 100) % 100;
if (num > 0)
cWords += String(num) + "/100"
else
cWords += "00/100"
return cWords;

 

Hope this solves the issue.

 

JR Boulay
Braniac
January 6, 2021
Acrobate du PDF, InDesigner et Photoshopographe
Nesa Nurani
Nesa NuraniCorrect answer
Inspiring
January 6, 2021

Since you found that online il adapt to that code , not sure if you just want to show 'Zero' or add 'Zero' to other text but you can use like this:

var f = this.getField("Number");
if(f.value == 0)
event.value = "Zero";
else event.value = ConvertToWords(f.value);

 

 

Sonny AR
Inspiring
February 22, 2023

Hello, I'm using the Number to Words script in Document JavaScript. Everything is working fine. But the problem is when the input amount is "blank" the result gives me "Dollars only.", So I just want to hide this.

 

"If the input field is blank, the results in words field should also be blank."

 

Note: I'm also using prefix script (Custom Format Script) in words field which is:

if (event.value) event.value = "Amount Payable - " + event.value;

 

Thank you

 

aTens = [ "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"];
aOnes = [ "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine",
  "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", 
  "Nineteen" ];

function ConvertToHundreds(num)
{
   var cNum, nNum;
   var cWords = "";

   num %= 1000;
   if (num > 99) {
      /* Hundreds. */
      cNum = String(num);
      nNum = Number(cNum.charAt(0));
      cWords += aOnes[nNum] + " Hundred";
      num %= 100;
      if (num > 0)
         cWords += " and "
   }
 
   if (num > 19) {
      /* Tens. */
      cNum = String(num);
      nNum = Number(cNum.charAt(0));
      cWords += aTens[nNum - 2];
      num %= 10;
      if (num > 0)
         cWords += "-";
   } 

   if (num > 0) {
      /* Ones and teens. */
      nNum = Math.floor(num);
      cWords += aOnes[nNum];
   }

   return cWords;
}

function ConvertToWords(num)
{
   var aUnits = [ "Thousand", "Million", "Billion", "Trillion", "Quadrillion" ];
   var cWords = (num >= 1 && num < 2) ? "Dollar Only." : "Dollars Only.";
   var nLeft = Math.floor(num);
   for (var i = 0; nLeft > 0; i++) { 
       if (nLeft % 1000 > 0) {
          if (i != 0)
             cWords = ConvertToHundreds(nLeft) + " " + aUnits[i - 1] + " " + cWords;
          else
             cWords = ConvertToHundreds(nLeft) + " " + cWords;
       }
       nLeft = Math.floor(nLeft / 1000);
   }
   num = Math.round(num * 100) % 100;
   if (num > 0)
      cWords += ConvertToHundreds(num) + "";
   else
      cWords += "";

   return cWords;
}