Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to a get the whole number without rounding in JavaScript

New Here ,
Oct 21, 2016 Oct 21, 2016

Hi, I'm a novice at JavaScript so please excuse me if my question is a bit simple.

In a PDF form, using Acrobat 9 Pro I'm trying to preform some mathematical calculations in a custom calculation script and I've hit a brick wall

I have a number in a variable, lets say 3.7. I want to return the whole number component, the "3" without rounding it up or down. I know I've done this in the past in AccessBasic, but I'm at a loss as to how to achieve it in JavaScript

I tried using toFixed(0) but that still round up. I've searched various JavaScript references but I'm unable to find a function that would help me.

The reason I'm trying to do it is rather convoluted, so I'm trying not to confuse you all by entering a long winded description of the problem. Any pointers would be gratefully received.

TOPICS
Acrobat SDK and JavaScript
2.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Oct 21, 2016 Oct 21, 2016

Try it in the JavaScript debugger.

3.2 - 0.5 is 2.7, this will be rounded to 3

Translate
Community Expert ,
Oct 21, 2016 Oct 21, 2016

Use something like this:

var a = 3.7;

Math.round(a - 0.5);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 21, 2016 Oct 21, 2016

Bernd,

Thank you for responding. Based on your answer, what happens is the value is say 3.2? That would then produce a result of 2 which is not what I'm after.

For example what I want is

40.6 Result = 40

40.1 Result = 40

40.9 Result = 40.

I just want to trim the number to it's whole value without rounding up or down.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 21, 2016 Oct 21, 2016

Try it in the JavaScript debugger.

3.2 - 0.5 is 2.7, this will be rounded to 3

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 21, 2016 Oct 21, 2016

I would use the Math.floor() method.

var a = 3.7;

Math.floor(a)

For a calculated field, one could add the following code after the calculation sets the field value:

event.value = Math.floor(event.value);

This will truncate the decimal value or round the number down to the next lower integer.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor

MDN JavaScript Reference Math.floor

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 27, 2023 Sep 27, 2023

Could you please help me?

 

Need help with PDF form.

 

I want Field 3 to take average of Fields 1 and 2, and display unrounded number.

 

Settings:

 

Field 1 - Number, zero decimal places

 

Field 2 - Number, Zero decimal places

 

Field 3 - Average, Field 1 and Field 2 selected.

 

My problem with these settings is that Field 3 shows the unrounded average and then if I click anywhere on the page, Field 3 changes to the rounded average.

 

  1. Then, I found this posted:

System.out.print( (double)(a + b) /2 );

 

This stops it from changing when I click on the page, but it still shows rounded.

 

How can I edit this script to stop rounding?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 28, 2023 Sep 28, 2023

That code will not work in a PDF form.

 

If "Field 3" is set to  peform an Average calculation, then remove any formatting and it will show the raw calculation value. If it is not, then there is something wrong with the form. 

Post the form so we can see the details. 

 

 

 

 

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 28, 2023 Sep 28, 2023
I posted the form in a previous reply.

Thanks,
Jasmine
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 28, 2023 Sep 28, 2023

That PDF was corrupted. Could you possibly recreate the form from scratch. And then post that PDF.  You might also see an improvement in the form behavior. 

 

 

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 28, 2023 Sep 28, 2023
LATEST
I actually reached out to a Developer who wrote the code for me.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 21, 2016 Oct 21, 2016

Bernd & gkaiseril,

Thank you both. I've tried both methods & get exactly the result I wanted. Thank you.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines