You are viewing limited content. For full access, please sign in.

Question

Question

Converting number values to words in forms

asked on June 1, 2015

Hi Guys,

 

Were formulating quotations with the ability to translate the summary  total price in numbers to words.

Ie. 1010 = One thousand ten. 

I have some codes in java and I  tried to use it using of custom html fields. Its working in html field in draft however we want to able to fill this value (word value) in the other field (single line field or Multiline field)  therefore we can also record the data in our SQL. Hope somebody could help how to add this code on my fields. 

Image of the fields :

JavaScript Code:

var th = ['','thousand','million', 'billion','trillion'];var dg = ['zero','one','two','three','four', 'five','six','seven','eight','nine']; var tn = ['ten','eleven','twelve','thirteen', 'fourteen','fifteen','sixteen', 'seventeen','eighteen','nineteen'];var tw = ['twenty','thirty','forty','fifty', 'sixty','seventy','eighty','ninety']; 
function toWords(s){      s = s.toString();     s = s.replace(/[\, ]/g,'');     if (s != parseFloat(s)) return 'not a number';     var x = s.indexOf('.');     if (x == -1) x = s.length;     if (x > 15) return 'too big';     var n = s.split('');     var str = '';     var sk = 0;     for (var i=0; i < x; i++)     {        if ((x-i)%3==2)         {            if (n[i] == '1')             {                str += tn[Number(n[i+1])] + ' ';                 i++;                 sk=1;            }            else if (n[i]!=0)             {                str += tw[n[i]-2] + ' ';                sk=1;            }        }        else if (n[i]!=0)         {            str += dg[n[i]] +' ';             if ((x-i)%3==0) str += 'hundred ';            sk=1;        }
        if ((x-i)%3==1)        {            if (sk) str += th[(x-i-1)/3] + ' ';            sk=0;        }    }    if (x != s.length)    {        var y = s.length;         str += 'point ';         for (var i=x+1; i<y; i++) str += dg[n[i]] +' ';    }    return str.replace(/\s+/g,' ');}

Tnx,

Cherry

 

0 0

Answer

APPROVED ANSWER
replied on June 1, 2015 Show version history

You can use the Javascript from this page. You can follow the steps on that page to make your own toword.js file (attached here as a .txt file, just rename the extension to .js) that you can place into C:\Program Files\Laserfiche\Laserfiche Forms\Forms\js

Then in your form, you can use the Javascript below

$(document).ready(function() {
  
  $.getScript('http://server/Forms/js/toword.js');
  
  $('.number input').on('change', function() {
    var a = toWords($(this).val());
    $('.words input').val(a);
    $('.wordsmulti textarea').val(a);
  });
  
});

Note that this assumes your number field uses the CSS class, number. I'm also using a single line field and multi-line field with CSS class names, words and wordsmulti respectively. Here are some examples of the output:

toword.txt (1.45 KB)
2 0

Replies

replied on October 14, 2017

I created a form to enter cheque details - Date, Order to, Amount and Amount in words. I found Alexander posting extremely helpful. However I need to make changes to the script toword.txt  to make it comply Australian Printed Cheques. 

I  replaced POINT with "Dollars and" and added a new array ndg to return digits for the cents. Added to the Return String "Cents ***" eg. now 123.56  looks like One Hundred Twenty Three Dollars and 56 Cents ****

The form calls a workflow that grabs the form tokens and use a simple merge fields Update Word Doc Activity to produce a Pdf. Created a new entry in the repository and attached the pdf. The user views the pdf in Laserfiche (web browser) to print a cheque. Simple....

Tip: Download attached file to avoid confusion cause the system will remember it is a Text file so renaming wont work. You need to open the file with Notepad. Select all and copy then choose Menu- Open a New Notepad and paste then Save As "Toword.js" with a File Type *.*    (Not *.TXT) copy it to C:\Program Files\Laserfiche\Laserfiche Forms\Forms\js - remember to change the "Server" in the script to your server.

toword.txt (1.79 KB)
2 0
replied on June 1, 2015

Here's a sample form I'm using

The number field uses the CSS class, number. The words field uses the CSS class, words. The words multiline field uses the CSS class, wordsmulti.

This Javascript works for me

$(document).ready(function() {
  
  $('.number input').on('change', function() {
    var a=0;
    a=convert($(this).val());
    $('.words input').val(a);
    $('.wordsmulti textarea').val(a);
  });
 
  var ones=['','one','two','three','four','five','six','seven','eight','nine'];
  var tens=['','','twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety'];
  var teens=['ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen'];

  function convert_trillions(num){
    if (num>=1000000000000){
      return convert_trillions(Math.floor(num/1000000000000))+" trillion "+convert_billions(num%1000000000000);
    }
    else {
      return convert_billions(num);
    }
  }
  
  function convert_billions(num){
    if (num>=1000000000){
      return convert_billions(Math.floor(num/1000000000))+" billion "+convert_millions(num%1000000000);
    }
    else {
      return convert_millions(num);
    }
  }
  
  function convert_millions(num){
    if (num>=1000000){
      return convert_millions(Math.floor(num/1000000))+" million "+convert_thousands(num%1000000);
    }
    else {
      return convert_thousands(num);
    }
  }

  function convert_thousands(num){
    if (num>=1000){
      return convert_hundreds(Math.floor(num/1000))+" thousand "+convert_hundreds(num%1000);
    }
    else{
      return convert_hundreds(num);
    }
  }

  function convert_hundreds(num){
    if (num>99){
      return ones[Math.floor(num/100)]+" hundred "+convert_tens(num%100);
    }
    else{
      return convert_tens(num);
    }
  }

  function convert_tens(num){
    if (num<10) return ones[num];
    else if (num>=10 && num<20) return teens[num-10];
    else{
      return tens[Math.floor(num/10)]+" "+ones[num%10];
    }
  }

  function convert(num){
    if (num==0) {
      return "zero";
    } else {
      return convert_trillions(num);
    }
  }
  
});
replied on June 1, 2015

Thanks I got it.

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.