Skip to main content
Participant
July 25, 2018
Question

Need help for number to words in adobe acrobat form

  • July 25, 2018
  • 1 reply
  • 329 views

//This is my code now.

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 == '1') {

                str += tn[Number(n[i + 1])] + ' ';

                i++;

                sk = 1;

            } else if (n != 0) {

                str += tw[n - 2] + ' ';

                sk = 1;

            }

        } else if (n != 0) {

            str += dg[n] + ' ';

            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 += 'AND '+ '/100 ';

        for (var i = x + 1; i < y; i++) str += dg[n] + ' ';

    }

    return str.replace(/\s+/g, ' ');

}

//for 150.23, it shows "one hundred fifty AND /100 two three ", I would like to make it to "one hundred fifty AND 23/100"

//Value s will only have two decimals. Is pre-cuted to two decimals by another function.

//I have been working on it but can't manage to find a solution.

//Help would be very appreciate as I'm beginner in programming.

//Thank you

This topic has been closed for replies.

1 reply

Thom Parker
Community Expert
Community Expert
July 26, 2018

You can find Number to text converter script on the internet. The JavaScript for this is nearly exactly the same as the Acrobat JavaScript. All you have to do is remove the references to web page elements. I did this for the bank check example (which is exactly what you've described) that can be download (by members) from here:

Search Downloads

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often