Click to See Complete Forum and Search --> : "Merging" fields for calculations


code_monkey
02-13-2003, 02:40 PM
I have a PDF form with separate boxes for entering numbers (like you see on machine-readable forms), and need to "merge" the numbers in three different boxes to form one number (2 0 0 becomes 200). I need to do this to several sets of boxes, add up the resulting numbers, and then take that total and "unmerge" it back into separate boxes. Any help out there?

LAwebTek
02-13-2003, 02:46 PM
I know how to do this with javascript if you were talking about using it with an html form, can you even write javascript functions and implement them in a PDF document?

Nevermore
02-13-2003, 02:49 PM
<script>
input1=document.form.text1.value;
input2=document.form.text2.value;
input3=document.form.text3.value;
merge=input1 + input2 + input3;
number1=parseInt(merge);
</script>

Will give you a number as number1, which is the three numbers joined. You can then add it as normal.

code_monkey
02-13-2003, 03:07 PM
Thank cijori. Now how do I take the result (which is a bunch of "merged" sets added together) and "unparse" them to put each number in a separate box?

Nevermore
02-13-2003, 03:28 PM
How many characters long will the final ouput be?

code_monkey
02-13-2003, 03:30 PM
Input and output are both 2.1 (2 whole numbers, 1 decimal).

Nevermore
02-13-2003, 03:41 PM
Sorry, it didnt actually matter.

<script language="Javascript" type="text/javascript">
<!--
function stringify() {
numberString = new String;
numberString = "" + number1;
numberSubString= new Array();
for (x=0; x<numberString.length; x++){
y = x + 1;
numberSubString[x]=numberString.substring(x,y)
}

}
//-->
</script>
This will give you single digit parts of a value called number1, and store them in variables called numberSubString[0], numberSubString[1] etc, with the number getting bigger depending how many numbers there are. All you then need to do is asign the text boxes this as their value. This method will put the decimal point as one of the values (if you feed it one) - if you dont want that just dont use its variable name. If you need help, PM me. Oh, and if you are using deciaml numbers, you need to use parseFloat() not parseInteger.

code_monkey
02-14-2003, 10:28 PM
I don't understand this syntax. I', okay up through the "for statement. I'm familiar with "for x = number to number, do this". It seems it says "for x = 0", then says x is less than the length of my number (?), then increments x by one. Also, if the number is stored in a one-dimensional array, why does it substring on [x,y]? Sorry -- I'm new to JavaScript and I guess it's more a syntax thing...

Nevermore
02-15-2003, 03:52 AM
Hope this helps:
The length of a string is the number of characters in it, or the length of an array is the number of elements in it.

It substrings on x,y because I wasn't sure how many digits I was isolating at a time, and I wanted to make it easier to modify. Too much coffee, I think.

Charles
02-15-2003, 04:12 AM
cijori,

Haven't you heard about the split() method in JavaScript? (http://developer.netscape.com/docs/manuals/js/client/jsref/string.htm#1194452) You'll also need to use the replace()method to get rid of the decimal point. (http://developer.netscape.com/docs/manuals/js/client/jsref/string.htm#1194258).

<script type="text/javascript">
<!--
alert('1234.5'.replace(/\./g, '').split(''));
// -->
</script>

Nevermore
02-15-2003, 04:15 AM
Yes, actually I tried that, but because the original value is not a string, it would not have worked. I guess after that i just forgot about it.