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

rounddown for a calculation

Explorer ,
Feb 03, 2022 Feb 03, 2022

ok after 2hrs of trying to research and trial and error ive come back to the community to help me out please. trying to get a number rounded down using custom calc. my formula is:

var v1 = this.getfield("STR"); var v2 = this.getfield("StrItem"); var v3 = this.getfield("RageStrMod"); event.value = roundDown((v1 + v2 /2)+ v3);

ive tried other formulas but nothing seems to work. any help is appriciated!

TOPICS
Create PDFs , General troubleshooting , How to , JavaScript , PDF forms
2.8K
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 ,
Feb 03, 2022 Feb 03, 2022

Thank you Nesa, I was a little fast on my update. Just copying the code.  Note that the code also does not correct for non-numerical inputs, but that's a different topic. 

Here's the correction:

 

var v1 = this.getField("STR").value; 
var v2 = this.getField("StrItem").value; 
var v3 = this.getField("RageStrMod").value; 
event.value = Math.floor((v1 + v2 /2)+ v3);

 

The Math.floor() function doesn't actually "round down" it simply removes any decimal value. There's no such thing as rounding up or down. There's just rounding. The "floor" and "ceiling" functions are just that, the floor or cieling of the decimal value. 

 

 

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

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 ,
Feb 03, 2022 Feb 03, 2022

The actual field values are missing from the calculation script.

Here's the fixed code

 

 

var v1 = this.getfield("STR").value; 
var v2 = this.getfield("StrItem").value; 
var v3 = this.getfield("RageStrMod").value; 
event.value = roundDown((v1 + v2 /2)+ v3);

 

 

 

 

There aren't any built-in functions named "roundDown". So I'm assuming that you have created this function yourself?

 

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
Explorer ,
Feb 03, 2022 Feb 03, 2022

thank you and no i was going off of https://helpx.adobe.com/sign/using/calculated-fields.html# under supported functions?

its part of official adobe website. but it's not working. is there a way to create a function to round 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 ,
Feb 03, 2022 Feb 03, 2022

You have error in your script, f in this.getfield need to be capital: this.getField.

To round down you can use 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 Expert ,
Feb 03, 2022 Feb 03, 2022

Thank you Nesa, I was a little fast on my update. Just copying the code.  Note that the code also does not correct for non-numerical inputs, but that's a different topic. 

Here's the correction:

 

var v1 = this.getField("STR").value; 
var v2 = this.getField("StrItem").value; 
var v3 = this.getField("RageStrMod").value; 
event.value = Math.floor((v1 + v2 /2)+ v3);

 

The Math.floor() function doesn't actually "round down" it simply removes any decimal value. There's no such thing as rounding up or down. There's just rounding. The "floor" and "ceiling" functions are just that, the floor or cieling of the decimal value. 

 

 

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
Explorer ,
Feb 03, 2022 Feb 03, 2022

PERFECT! thank you so much! the math was off a lil had to put another () to add before dividing but thats my bad. again thank you tons!

 

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
Enthusiast ,
Feb 03, 2022 Feb 03, 2022
quote

The Math.floor() function doesn't actually "round down" it simply removes any decimal value. 

 

 


By @Thom Parker

Actually you are wrong there, Math.trunc() (which i belive is not supported in acrobat) would be for removing decimals, Math.floor() does round down to nearest integer.

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 ,
Feb 07, 2022 Feb 07, 2022
LATEST

True, the Math.trunc function specifically removes the decimal portion of a number, but that doesn't mean Math.floor rounds down. It doesn't. The floor function returns the next smallest integer. Which is literally the same thing as truncating for positive decimals. 

 

 

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