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

Help with rounding writing a function for rounding down.

New Here ,
Jun 05, 2017 Jun 05, 2017

I need some help creating a formula for rounding down a field in a form I'm creating.  I need the field to take the numbers from 2 separate fields and add them together subtract 10 and then divide by 2.

Something like (Field1+Field2-10)/2  and then round down to the nearest whole number so 4.5 goes to 4 and so on.

Also I dont know if it would work differently but if it does i need it to work with negative numbers so -3.5 rounds to 4

If anyone could help me with this i would be very appreciative.

TOPICS
Acrobat SDK and JavaScript , Windows
1.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 , Jun 05, 2017 Jun 05, 2017

Try this code as the field's custom calculation script:

var result = ((Number(this.getField("Field1").value)+Number(this.getField("Field2").value))-10)/2;

event.value = Math.round(Math.abs(result));

Translate
Community Expert ,
Jun 05, 2017 Jun 05, 2017

Try this code as the field's custom calculation script:

var result = ((Number(this.getField("Field1").value)+Number(this.getField("Field2").value))-10)/2;

event.value = Math.round(Math.abs(result));

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 ,
Jun 05, 2017 Jun 05, 2017

This kinda works, is there a way to make it work for negative numbers, like -3.5 to -4

Thanks btw for the help.

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 05, 2017 Jun 05, 2017

That is more tricky because by default the rounds negative numbers that end with .5 down, while positive numbers get rounded up... You will need to use a custom rounding function to do that.

And to keep the negative numbers negative just remove the Math.abs() part of the code I posted above.

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 ,
Jun 05, 2017 Jun 05, 2017

Or did you mean that -3.5 rounds to -4?

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 ,
Jun 05, 2017 Jun 05, 2017

Yes, so -3.5 to -4

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 ,
Jun 05, 2017 Jun 05, 2017

You might have to use the Math.floor method and not the Math.round method.

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 05, 2017 Jun 05, 2017

That would work, but it would round down any number... So 3.99 will become 3.0. I'm not sure that's what the OP is after.

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 ,
Jun 05, 2017 Jun 05, 2017
LATEST

Worked out perfectly, thanks for all the help guys.

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