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

Round down to 0.5

New Here ,
Jun 22, 2018 Jun 22, 2018

Copy link to clipboard

Copied

I can't get the JavaScript to round down to 0.5. In Excel, FLOOR.MATH(A1,0.5) does what I want, but it doesn't do so in Adobe.  For example, I need 1.1 to 1.4 to round down to 1.0, and 1.6 to 1.9 to round down to 1.5.  I have the formula event.value=Math.floor(this.getField("CPE1").value,.5) in Adobe, but this just returns 1.1 to 1.9 to 1.0.  Any ideas?

TOPICS
Acrobat SDK and JavaScript , Windows

Views

165

Likes

Translate

Translate

Report

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 22, 2018 Jun 22, 2018

Copy link to clipboard

Copied

You can't just invent parameters and hope for them to work... You need to consult the documentation of the methods you're using.
In this case, there's no built-in command that does it for you in the core syntax, but it's pretty simple to write a function that will do it, like this:

function floorHalf(num) {

    return Math.floor(num*2)/2;

}

You would then call it like this, for example:

floorHalf(3.81)

and it will return 3.5 .

Likes

Translate

Translate

Report

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 22, 2018 Jun 22, 2018

Copy link to clipboard

Copied

Thanks for the reply. Sorry if it came across that I was inventing parameters and hoping they would work. The Floor.Math function I posted did exactly what I wanted it to do in Excel, and in my ignorance, I was hoping that it would work the same way in JavaScript.  I simply don't have the experience in JavaScript to know that it wouldn't work the same way. I'm not sure I understand your answer. Do you mean that I'd have to list every possible number in order to obtain the value I want?  Would I have to do separate lines for 1.1, 1.2, 1.3, 1.4, 1.5, 1.6.1.7, etc.?

Right now, what I have is event.value=FLOOR(this.getField("CPE1").value,.5).

The CPE1 field is what needs to be rounded.  In Excel, the .5 after the comma makes it round down to the nearest .5.  I'm sorry that I don't understand

Likes

Translate

Translate

Report

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 22, 2018 Jun 22, 2018

Copy link to clipboard

Copied

No, of course not. You just need to place the function itself as a doc-level script and then change your code to:

event.value = floorHalf(Number(this.getField("CPE1").value));

Likes

Translate

Translate

Report

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 22, 2018 Jun 22, 2018

Copy link to clipboard

Copied

Thank you again, but I'm afraid my ignorance again challenges me.  I'm not sure what you mean by placing the functino as a document-level script.  Does that mean putting the formula below at the document level?  I think I added the formula to the document level, but I don't that's exactly what you mean.  Sorry.

Likes

Translate

Translate

Report

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 22, 2018 Jun 22, 2018

Copy link to clipboard

Copied

LATEST

I figured it out thanks to you - finally understood that you meant the previous function you sent.  Thanks so Much!

Likes

Translate

Translate

Report

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