These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Simple array calculation

miQlo
Registered: Jul 13 2010
Posts: 44
Answered

I need to sum a couple of positions in a array. I need 1+2 to equal 3 but with my code it returns 12. Noob error I assume but I can't find an answer in any reference.

I've put this in the custom calculation script.
event.value = AData[this.getField("ABox").value].a + BData[this.getField("BBox").value].b

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
In JavaScript the '+' operator functions as both the addition and string concatenation operation depending upon whether the value being processed is a number or character string. Note a numeric value can also be a character string. So how the '+' operator works depends upon how JavaScript treats the values being processed. You need to make sure that JavaScript treats the values as numbers and not character strings of numbers.

JavaScript has the 'Number' constrictor that will force a character strings of numbers to a numeric string.

George Kaiser

miQlo
Registered: Jul 13 2010
Posts: 44
OK, I've put all my numbers in between "", that forces the string to be characters I assume. The thing is that I have a very long list with data and it would take me all day to remove all the "", how do I force it to read it as an numeric string, or how do I use the number constrictor that you suggest? I've searched with no result.

Another thing, I've noticed that it reads 01000, 02000, 03000... as octal, that could have given me an headache if not noticed this early.
miQlo
Registered: Jul 13 2010
Posts: 44
I've solved it by removing the quotes now but it would be nice to have an answer anyway.
try67
Expert
Registered: Oct 30 2008
Posts: 2398
To use the Number constructor you simply do the following:

var n = Number(string);
If the variable "string" can't be converted to a number, n will be NaN (Not a Number), which you can verify with this boolean-returning method:
isNaN(n)

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

miQlo
Registered: Jul 13 2010
Posts: 44
Thanks guys, that helped.
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
Beware you need to make sure each value is a number or zero if a null string, otherwise and character string for any value will force JavaScript to assume you want to concatenate from that item on. For 2 numbers this might not be a problem, but for a series of numbers, this can cause real havoc and be difficult to debug.

See [url=https://acrobat.com/#d=BTBoKOwsJb0KdpT-*8SZqw|Sum vs. Plus[/url]for a working form example.

George Kaiser