Skip to main content
Howard5E77
Inspiring
December 17, 2020
Answered

Truncating (x/10)

  • December 17, 2020
  • 2 replies
  • 1380 views

I have a variable, X.

I need to divide that variable by 10.

X/10 is not the problem. I can do that with the "Calculate" tab.

The problem is that I need a whole number based on the truncated value of (X/10).

I have tried TRUNC(X/10). I still get a decimal.

I have teied FLOOR(X/10). Same response.

How do I get a truncated value, and what do I type where tomake it work?

Thanks!

This topic has been closed for replies.
Correct answer Howard5E77

I didn't know where you were running the code from, so I assumed it was in the target event field.. that is why I am using event.value in the absence of a declared variable.

 

But If the total value is the result of a calculation that is not performed in the target event field and "Input" is another text field object,  then declare your variable for the field named "Input" like:

 

var Input = this.getField("Input").value/10;
event.value = parseInt(Input, 10);

 

 


Yay! Thank you so much!

Just so I understand whata's happening:

(1) We chnage the string variable "Input" into a numeric value with Get.this

(2) We then used the new numeric value to obtain the truncated vallue.

 

Yeah.. In BASIC, I would have made the inout variable a numberic value, but in the script in Acrobat (because it is placed in a calculation field based on a non-numeric input field), I had to convert AND do thew math.

 

Yeah! I get that.

 

Thank yu again!

2 replies

try67
Braniac
December 17, 2020

You can do it like this:

Math.floor(X/10)

Howard5E77
Inspiring
December 18, 2020

Math.floor(x/10) DOES work visibly,  but the value is still showing a single decimal. I need my calculations based on that number (and others like it) to be unaffected by Phantom Fractions. Is there a way to keep the truncated whole number as the value?

try67
Braniac
December 18, 2020

Use it in the field's calculation script, then.

ls_rbls
Braniac
December 17, 2020

What works on my end is a custom format script:

 

 

event.value = util.printf("%.0f", event.value/10);

 

 

I am using the util.printf() method.  This is not the same as truncating it rounds up the event value in the target field and appear as a whole number .

 

Is this what you're inquiring about?

Howard5E77
Inspiring
December 19, 2020

I don't think so. I need to eliminate the Phantom Fractions completely, so that a value of 38/10 = 3 with no decimal hiding anywhere, or 33/10 = 3 with no decimal. I'm not wanting to round. Iused to do this in BASIC all the time with the INT(X) command.

ls_rbls
Braniac
December 19, 2020

Well, in that case, since you 're familiarized with INT(X) in BASIC, then you can employ the parseInt() method using 10 as the base number to convert a string to a whole number using Acrobat JavaScript.

 

I am using a custom format script in the event target field like this:

 

 

var f = event.value/10;
event.value = parseInt(f, 10);

 

 

See more parse methods here: https://gomakethings.com/converting-strings-to-numbers-with-vanilla-javascript/