Abobe Livecycle Form: Number to word conversion

1,332 views
Skip to first unread message

RajaSekar T

unread,
Nov 16, 2012, 4:08:26 AM11/16/12
to live...@googlegroups.com
Hi All

The group is doing great.

I am beginner for Adobe Livecycle workbench while doing form design I
am facing challenge in coverting numeric value to words. For ex. 1100
should be displayed as “One Thousand and One Hundred”. Any of you
would have came acorss this kind of scenario in your earlier projects

Kindly provide solution for the same.

Thanks
Rajasekaran T

fred.pantalone

unread,
Nov 17, 2012, 1:02:36 PM11/17/12
to live...@googlegroups.com
Hi there,

I did some work for a bank several years ago that included this type of functionality. I found a Java library (open source, distributed by IBM if I remember correctly) and did it server side. I'l look up the library info on my work computer and post it here on Monday. If you can do it server-side then you're more likely to find a library to accomplish the task. If you have to do it client-side, i.e. on the form, then I imagine that it will be tougher to find a JavaScript library but I haven't looked for one. Have you?

Fred

fred.pantalone

unread,
Nov 17, 2012, 1:06:01 PM11/17/12
to live...@googlegroups.com
I should have mentioned that, worst case, you can always write your own function to do this. It wouldn't be super-difficult but it is extra work.

Fred

Duane Nickull

unread,
Nov 17, 2012, 2:39:50 PM11/17/12
to Adobe LiveCycle Developers
import java.text.DecimalFormat;

public class EnglishNumberToWords {

private static final String[] tensNames = {
"",
" ten",
" twenty",
" thirty",
" forty",
" fifty",
" sixty",
" seventy",
" eighty",
" ninety"
};

private static final String[] numNames = {
"",
" one",
" two",
" three",
" four",
" five",
" six",
" seven",
" eight",
" nine",
" ten",
" eleven",
" twelve",
" thirteen",
" fourteen",
" fifteen",
" sixteen",
" seventeen",
" eighteen",
" nineteen"
};

private static String convertLessThanOneThousand(int number) {
String soFar;

if (number % 100 < 20){
soFar = numNames[number % 100];
number /= 100;
}
else {
soFar = numNames[number % 10];
number /= 10;

soFar = tensNames[number % 10] + soFar;
number /= 10;
}
if (number == 0) return soFar;
return numNames[number] + " hundred" + soFar;
}


public static String convert(long number) {
// 0 to 999 999 999 999
if (number == 0) { return "zero"; }

String snumber = Long.toString(number);

// pad with "0"
String mask = "000000000000";
DecimalFormat df = new DecimalFormat(mask);
snumber = df.format(number);

// XXXnnnnnnnnn
int billions = Integer.parseInt(snumber.substring(0,3));
// nnnXXXnnnnnn
int millions = Integer.parseInt(snumber.substring(3,6));
// nnnnnnXXXnnn
int hundredThousands = Integer.parseInt(snumber.substring(6,9));
// nnnnnnnnnXXX
int thousands = Integer.parseInt(snumber.substring(9,12));

String tradBillions;
switch (billions) {
case 0:
tradBillions = "";
break;
case 1 :
tradBillions = convertLessThanOneThousand(billions)
+ " billion ";
break;
default :
tradBillions = convertLessThanOneThousand(billions)
+ " billion ";
}
String result = tradBillions;

String tradMillions;
switch (millions) {
case 0:
tradMillions = "";
break;
case 1 :
tradMillions = convertLessThanOneThousand(millions)
+ " million ";
break;
default :
tradMillions = convertLessThanOneThousand(millions)
+ " million ";
}
result = result + tradMillions;

String tradHundredThousands;
switch (hundredThousands) {
case 0:
tradHundredThousands = "";
break;
case 1 :
tradHundredThousands = "one thousand ";
break;
default :
tradHundredThousands = convertLessThanOneThousand(hundredThousands)
+ " thousand ";
}
result = result + tradHundredThousands;

String tradThousand;
tradThousand = convertLessThanOneThousand(thousands);
result = result + tradThousand;

// remove extra spaces!
return result.replaceAll("^\\s+", "").replaceAll("\\b\\s{2,}\\b", " ");
}

/**
* testing
* @param args
*/
public static void main(String[] args) {
System.out.println("*** " + EnglishNumberToWords.convert(0));
System.out.println("*** " + EnglishNumberToWords.convert(1));
System.out.println("*** " + EnglishNumberToWords.convert(16));
System.out.println("*** " + EnglishNumberToWords.convert(100));
System.out.println("*** " + EnglishNumberToWords.convert(118));
System.out.println("*** " + EnglishNumberToWords.convert(200));
System.out.println("*** " + EnglishNumberToWords.convert(219));
System.out.println("*** " + EnglishNumberToWords.convert(800));
System.out.println("*** " + EnglishNumberToWords.convert(801));
System.out.println("*** " + EnglishNumberToWords.convert(1316));
System.out.println("*** " + EnglishNumberToWords.convert(1000000));
System.out.println("*** " + EnglishNumberToWords.convert(2000000));
System.out.println("*** " + EnglishNumberToWords.convert(3000200));
System.out.println("*** " + EnglishNumberToWords.convert(700000));
System.out.println("*** " + EnglishNumberToWords.convert(9000000));
System.out.println("*** " + EnglishNumberToWords.convert(9001000));
System.out.println("*** " + EnglishNumberToWords.convert(123456789));
System.out.println("*** " + EnglishNumberToWords.convert(2147483647));
System.out.println("*** " + EnglishNumberToWords.convert(3000000010L));

/*
*** zero
*** one
*** sixteen
*** one hundred
*** one hundred eighteen
*** two hundred
*** two hundred nineteen
*** eight hundred
*** eight hundred one
*** one thousand three hundred sixteen
*** one million
*** two millions
*** three millions two hundred
*** seven hundred thousand
*** nine millions
*** nine millions one thousand
*** one hundred twenty three millions four hundred
** fifty six thousand seven hundred eighty nine
*** two billion one hundred forty seven millions
** four hundred eighty three thousand six hundred forty seven
*** three billion ten
**/
}
}



***********************************
Technoracle Advanced Systems Inc.
Consulting and Contracting; Proven Results!
i. Neo4J, PDF, Java, LiveCycle ES, Flex, AIR, CQ5 & Mobile
b. http://technoracle.blogspot.com
t. @duanechaos
"Don't fear the Graph! Embrace Neo4J"
>--
>You received this message because you are subscribed to the Google Groups
>"Adobe LiveCycle Developers" group.
>To post to this group, send email to live...@googlegroups.com.
>To unsubscribe from this group, send email to
>livecycle+...@googlegroups.com.
>For more options, visit this group at
>http://groups.google.com/group/livecycle?hl=en.
>


RajaSekar T

unread,
Nov 19, 2012, 7:20:21 AM11/19/12
to live...@googlegroups.com
Hi All

First thanks for all replies. I have sent also 4 mails ealier
regarding many other issues. This is the first time I got reply.
Thanks for replying onceagain.

Fred
I am looking for server side script only

Duane
Thanks for the script itself

Moreover I have found a FormCalc function named WordNum() which
provide the desired result without any script. But there is a
challenge. It works only in US currency format, I need it in India
format, I googled lot not able to find any solution. Pls throw some
light in regional currency conversion options. If I didnt find
anything then i have use Duane scripts

fred.pantalone

unread,
Nov 19, 2012, 11:34:21 AM11/19/12
to live...@googlegroups.com
Here is the library I was referring to. It should give you everything you need:


Fred

Rob McDougall

unread,
Nov 19, 2012, 1:44:56 PM11/19/12
to live...@googlegroups.com
FormCalc has a built-in function called WordNum().  All you need to do is perform your calculation in FormCalc.

IMO, FormCalc is under-appreciated.  It's faster than Javascript and has a lot of useful functionality built-in that Javascript doesn't.  It's a trade-off between a general, standardized language like Javascript vs. a specialized non-standardized language like FormCalc.

Rob McDougall | Senior Technical Architect | 4Point | www.4Point.com

Duane Nickull

unread,
Nov 19, 2012, 2:20:50 PM11/19/12
to Adobe LiveCycle Developers
Very cool Rob!    I have to admit that I have never really looked deeply at formCalc as being familiar with JS made me lazy.  WordNum() certainly looks more elegant that writing YAJC (Yet another Java Class).

The only thing is appears worse than not getting an answer on this list is getting three.

Cheers!

Duane Nickull
***********************************
Technoracle Advanced Systems Inc.
Consulting and Contracting; Proven Results!
i.  Neo4J, PDF, Java, LiveCycle ES, Flex, AIR, CQ5 & Mobile
t.  @duanechaos
"Don't fear the Graph!  Embrace Neo4J"


--
You received this message because you are subscribed to the Google Groups "Adobe LiveCycle Developers" group.
To view this discussion on the web visit https://groups.google.com/d/msg/livecycle/-/ssi7jmm6AX0J.

Brooks, Craig

unread,
Nov 26, 2012, 9:31:26 AM11/26/12
to live...@googlegroups.com
Rajasekaran,

You can test and play with this by saving this file as HTML on a local server (like XAMPP). You can carve out the J/S to meet your needs. The "onkey" events show the results real-time on a web page.

Regards,
Craig Brooks

================================================
BEGIN FILE
================================================

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>

<script type="text/javascript">
function update(){
var bigNumArry = new Array('', ' thousand', ' million', ' billion', ' trillion', ' quadrillion', ' quintillion');

var output = '';
var numString = document.getElementById('number').value;
var finlOutPut = new Array();

if (numString == '0') {
document.getElementById('container').innerHTML = 'Zero';
return;
}

if (numString == 0) {
document.getElementById('container').innerHTML = 'messeg tell to enter numbers';
return;
}

var i = numString.length;
i = i - 1;

//cut the number to grups of three digits and add them to the Arry
while (numString.length > 3) {
var triDig = new Array(3);
triDig[2] = numString.charAt(numString.length - 1);
triDig[1] = numString.charAt(numString.length - 2);
triDig[0] = numString.charAt(numString.length - 3);

var varToAdd = triDig[0] + triDig[1] + triDig[2];
finlOutPut.push(varToAdd);
i--;
numString = numString.substring(0, numString.length - 3);
}
finlOutPut.push(numString);
finlOutPut.reverse();

//conver each grup of three digits to english word
//if all digits are zero the triConvert
//function return the string "dontAddBigSufix"
for (j = 0; j < finlOutPut.length; j++) {
finlOutPut[j] = triConvert(parseInt(finlOutPut[j]));
}

var bigScalCntr = 0; //this int mark the million billion trillion... Arry

for (b = finlOutPut.length - 1; b >= 0; b--) {
if (finlOutPut[b] != "dontAddBigSufix") {
finlOutPut[b] = finlOutPut[b] + bigNumArry[bigScalCntr] + ' , ';
bigScalCntr++;
}
else {
//replace the string at finlOP[b] from "dontAddBigSufix" to empty String.
finlOutPut[b] = ' ';
bigScalCntr++; //advance the counter
}
}

//convert The output Arry to , more printable string
for(n = 0; n<finlOutPut.length; n++){
output +=finlOutPut[n];
}

document.getElementById('container').innerHTML = output;//print the output
}

//simple function to convert from numbers to words from 1 to 999
function triConvert(num){
var ones = new Array('', ' one', ' two', ' three', ' four', ' five', ' six', ' seven', ' eight', ' nine', ' ten', ' eleven', ' twelve', ' thirteen', ' fourteen', ' fifteen', ' sixteen', ' seventeen', ' eighteen', ' nineteen');
var tens = new Array('', '', ' twenty', ' thirty', ' forty', ' fifty', ' sixty', ' seventy', ' eighty', ' ninety');
var hundred = ' hundred';
var output = '';
var numString = num.toString();

if (num == 0) {
return 'dontAddBigSufix';
}
//the case of 10, 11, 12 ,13, .... 19
if (num < 20) {
output = ones[num];
return output;
}

//100 and more
if (numString.length == 3) {
output = ones[parseInt(numString.charAt(0))] + hundred;
output += tens[parseInt(numString.charAt(1))];
output += ones[parseInt(numString.charAt(2))];
return output;
}

output += tens[parseInt(numString.charAt(0))];
output += ones[parseInt(numString.charAt(1))];

return output;
}
</script>

</head>
<body>

<input type="text"
id="number"
size="70"
onkeyup="update();"
/*this code prevent non numeric letters*/
onkeydown="return (event.ctrlKey || event.altKey
|| (47<event.keyCode && event.keyCode<58 && event.shiftKey==false)
|| (95<event.keyCode && event.keyCode<106)
|| (event.keyCode==8) || (event.keyCode==9)
|| (event.keyCode>34 && event.keyCode<40)
|| (event.keyCode==46) )"/>
<br/>
<div id="container">Here The Numbers Printed</div>
</body>
</html>

================================================
END FILE
================================================
--
You received this message because you are subscribed to the Google Groups "Adobe LiveCycle Developers" group.

Stan Batcha

unread,
Mar 5, 2015, 5:12:54 PM3/5/15
to live...@googlegroups.com
Hi all, I have just joined the group and looking forward have a good time.
thank you to all the contributors so far. I was researching the NumWord conversion then accidentally I stumble here.
Please keep the fire burning.
Stan
Reply all
Reply to author
Forward
0 new messages