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

Prevent rounding in number fields

Community Beginner ,
May 17, 2021 May 17, 2021

Hello to all,

using Acrobat Pro 2017 (Classic Release), I created a fillable form. In the form I have a column with number fields that have the following field properties according to the screenshots:

adobe_form_01.png

 

adobe_form_02.png

 

As expected, the form contains a 0.

However, numbers with commas can still be entered. In the PDF form, these numbers are then rounded, e.g. 1.99 becomes 2 (compare screenshots):

adobe_form_03a.png

 

adobe_form_03b.png

 

How can I prevent rounding in Acrobat Pro or prevent commas from being entered in the field?

I am already very grateful for tips, ideas and possibly code examples!

I probably won't be able to get around JavaScript.

Regards, Daniel Schunk

TOPICS
PDF forms
5.9K
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
1 ACCEPTED SOLUTION
Community Expert ,
May 17, 2021 May 17, 2021

The entered values are not actually rounded, they are only formatted in a way that hides the decimal digits, because of the Number format you've selected. If you click the field after exiting it, you'll see that the value you entered is still there. Also, if you use it in a calculation it will use the entered value, not the displayed one.

So how to prevent the user from entering those decimals? You can't do it with the Number setting under Format, but you can by setting your own Keystroke script (under Properties - Format - Custom). For example, you can use this one:

event.rc = /^\d*$/.test(event.change);

It will only allow the user to enter digits into the field.

View solution in original post

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 ,
May 17, 2021 May 17, 2021

The entered values are not actually rounded, they are only formatted in a way that hides the decimal digits, because of the Number format you've selected. If you click the field after exiting it, you'll see that the value you entered is still there. Also, if you use it in a calculation it will use the entered value, not the displayed one.

So how to prevent the user from entering those decimals? You can't do it with the Number setting under Format, but you can by setting your own Keystroke script (under Properties - Format - Custom). For example, you can use this one:

event.rc = /^\d*$/.test(event.change);

It will only allow the user to enter digits into the field.

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 ,
May 18, 2021 May 18, 2021

Hello, try67,

thank you very much for the quick help.

It worked with the code line

event.rc = /^\d*$/.test(event.change);

 

However, I had first put it in the "Custom formatting script" section. That was not correct.

It only works in the "Custom key input script" section, I found.

 

More generally, I must note that the documentation on JavaScript in PDF at Adobe is quite large, but not as immediately obvious as at Microsoft, for example.

 

Best regards,

Daniel

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 ,
May 18, 2021 May 18, 2021

I wrote it needs to go under the Custom Keystroke Script...

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 ,
Mar 02, 2022 Mar 02, 2022

This is great - I had the same problem.  Is there a way to adjust this to allow input of negative numbers?

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 ,
Mar 02, 2022 Mar 02, 2022

Sure. Change it to this:

 

event.rc = /^-?\d*$/.test(event.change);

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 ,
Mar 02, 2022 Mar 02, 2022

Awesome!!!  Thank you for the help (and so quickly!).   

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
Explorer ,
Sep 06, 2022 Sep 06, 2022
quote

The entered values are not actually rounded, they are only formatted in a way that hides the decimal digits, because of the Number format you've selected. If you click the field after exiting it, you'll see that the value you entered is still there. Also, if you use it in a calculation it will use the entered value, not the displayed one.

So how to prevent the user from entering those decimals? You can't do it with the Number setting under Format, but you can by setting your own Keystroke script (under Properties - Format - Custom). For example, you can use this one:

event.rc = /^\d*$/.test(event.change);

It will only allow the user to enter digits into the field.


By @try67

Is it posible to use displayed number in calculation? If it is how?

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
Explorer ,
Jun 30, 2023 Jun 30, 2023

Hello @try67 ,

 

I hope you don't mind me jumping in this thread, but this is the script I was looking for! Do you know how I can use this code but with a comma instead of a period for French decimal format?

 

And also, how do I display the characters showing decimals? It is now only showing a single digit, rather than a whole number + 2 decimals.

 

Your help is greatly appreciated!

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 ,
Jun 30, 2023 Jun 30, 2023

The whole point of this thread was to prevent the user from entering decimals... If you want to allow that (no matter in what format) then just use the built-in Number option, and then select the pattern you want it to have.

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 ,
Jan 16, 2024 Jan 16, 2024

How do I use a script with the below?

I need for 2 decimals places to show with no rounding. 

EX: the total is 73.449, right now it's rounding to 73.45. I want it just to show 73.44. 

 

 

 

// Get first field value

var v1 = getField("Payroll6").value;


// Get second field value

var v2 = getField("Payroll7").value;


// Set this field value equal to the difference

event.value = (v1 * v2);

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 ,
Jan 16, 2024 Jan 16, 2024

That is rounding, just down. You can do it using this code:

 

event.value = roundDown(v1 * v2, 2)

function roundDown(v, nDecimals) {
	return Math.floor(v * Math.pow(10, nDecimals)) / Math.pow(10, nDecimals);
}
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 ,
Feb 27, 2024 Feb 27, 2024

Thank you so much, this worked perfectly.



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 ,
Apr 04, 2025 Apr 04, 2025

how do you prevent rounding. Say you want the text to show 0.936 and do not want it to round up to 0.94 even if the total dollars are correct in the math formula total, I want the number field to show the 0.936 instead of the 0.94

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 ,
Apr 04, 2025 Apr 04, 2025
LATEST

never mind. I figured it out. I just changed the display from 2 decimals 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