Rounding to the Nearest 5, but making 5 come back as 5 instead of 10

Community Beginner ,
Sep 16, 2020

Copy link to clipboard

Copied

Hi,

 

I used the following script:

Math.ceil(N / 5) * 5;

 

But it's not quite giving me what I want. For instance "5" returns "10", but I need it to return "5"

I need it to look like the following:

0.01 - 5.00 = 5

5.01 - 10.00 = 10

10.01 - 15.00 = 15

15.01 - 20.00 = 20

Adobe Community Professional
Correct answer by ls_rbls | Adobe Community Professional

Using the script below gives you the results that you're looking with any of the values in those ranges.

 

I may be wrong, but is working on my end.

 

var f = Math.ceil(event.target.value / 5) * 5;

event.value = util.printf("%.0f", f);
TOPICS
Acrobat SDK and JavaScript, Create PDFs, Edit and convert PDFs, PDF forms

Views

99

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

Rounding to the Nearest 5, but making 5 come back as 5 instead of 10

Community Beginner ,
Sep 16, 2020

Copy link to clipboard

Copied

Hi,

 

I used the following script:

Math.ceil(N / 5) * 5;

 

But it's not quite giving me what I want. For instance "5" returns "10", but I need it to return "5"

I need it to look like the following:

0.01 - 5.00 = 5

5.01 - 10.00 = 10

10.01 - 15.00 = 15

15.01 - 20.00 = 20

Adobe Community Professional
Correct answer by ls_rbls | Adobe Community Professional

Using the script below gives you the results that you're looking with any of the values in those ranges.

 

I may be wrong, but is working on my end.

 

var f = Math.ceil(event.target.value / 5) * 5;

event.value = util.printf("%.0f", f);
TOPICS
Acrobat SDK and JavaScript, Create PDFs, Edit and convert PDFs, PDF forms

Views

100

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
Sep 16, 2020 0
Enthusiast ,
Sep 16, 2020

Copy link to clipboard

Copied

Whit that calculation they all will return 5.

To get results you want, you need something like this: Math.ceil(N/5)*X , X being field where you input first value (0.01, 5.01. 10.01...etc)

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
Reply
Loading...
Sep 16, 2020 1
Community Beginner ,
Sep 16, 2020

Copy link to clipboard

Copied

Basically, I'm trying to round up to the nearest 5, but when the info is 5, I want it to stay at 5 and not round up to 10.

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
Reply
Loading...
Sep 16, 2020 0
Enthusiast ,
Sep 16, 2020

Copy link to clipboard

Copied

So those are range and not substractions, In that case your code should work fine, there must be something else in your file. Check field calculation order maybe.

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
Reply
Loading...
Sep 16, 2020 0
Adobe Community Professional ,
Sep 16, 2020

Copy link to clipboard

Copied

Please define what you're trying to do.

 

You said that you're using a custom calculation script. Based on what you posted your rounding up a number that is divided by 5, then you multply that result by 5.

 

Then in the examples below you're using substraction. And not only that is a subsctraction but you're also substracting a larger number from a smaller number; the larger number also has a negative value.  So you're ccreating a confusion here.

 

If all you need is to avoid the zeroes appearing to the right of the decimal point use the utilprintf() method and if you don't want anything to be rounded up , nand all you want is to truncate a number that has decimal value then use the toFixed() method.

 

For the util.printf() method you should use Something like this: 

 

event.value = util.printf("%.0f", event.target.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
Reply
Loading...
Sep 16, 2020 0
Community Beginner ,
Sep 16, 2020

Copy link to clipboard

Copied

Sorry, the lower values are not a subtraction, but a range. I need all numbers between 0.01 to 5.00 to return the value of "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
Reply
Loading...
Sep 16, 2020 0
Adobe Community Professional ,
Sep 16, 2020

Copy link to clipboard

Copied

What does you get when you use N / 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
Reply
Loading...
Sep 16, 2020 1
Community Beginner ,
Sep 16, 2020

Copy link to clipboard

Copied

It doesn't round to the nearest 5 at all.

 

Screen Shot 2020-09-16 at 10.20.09 AM.png

 

Screen Shot 2020-09-16 at 10.20.53 AM.png

 

Screen Shot 2020-09-16 at 10.22.06 AM.png

 

Screen Shot 2020-09-16 at 10.22.52 AM.png

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
Reply
Loading...
Sep 16, 2020 0
Adobe Community Professional ,
Sep 16, 2020

Copy link to clipboard

Copied

So you get 2 for 5.0 / 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
Reply
Loading...
Sep 16, 2020 0
Most Valuable Participant ,
Sep 16, 2020

Copy link to clipboard

Copied

Something's wrong there. This formula returns 5:

Math.ceil(5.0/5)*5

 

Are the values in those fields calculated? If so, they might not be what you think they are...

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
Reply
Loading...
Sep 16, 2020 0
Community Beginner ,
Sep 16, 2020

Copy link to clipboard

Copied

Yes, they are calculated, and I wondered if the calculation was throwing it off, but the formula that ls_rbls gave below works perfectly. 🙂

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
Reply
Loading...
Sep 16, 2020 0
Adobe Community Professional ,
Sep 16, 2020

Copy link to clipboard

Copied

Using the script below gives you the results that you're looking with any of the values in those ranges.

 

I may be wrong, but is working on my end.

 

var f = Math.ceil(event.target.value / 5) * 5;

event.value = util.printf("%.0f", f);

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
Reply
Loading...
Sep 16, 2020 0
Adobe Community Professional ,
Sep 16, 2020

Copy link to clipboard

Copied

This is a custom calucation script . 

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
Reply
Loading...
Sep 16, 2020 0
Community Beginner ,
Sep 16, 2020

Copy link to clipboard

Copied

That worked perfectly! When you say 'custom calculation script'... what do you mean?

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
Reply
Loading...
Sep 16, 2020 1
Adobe Community Professional ,
Sep 16, 2020

Copy link to clipboard

Copied

I meant that you run it in the custom calculation script section of the field where you want this event to happen.

 

I specified that just in case you were also trying to attempt to run it as a custom key stroke script, validation script , or even as a mouse-up action. Sometimes us users make that mistake and try to execute the scripts in the wrong place.

 

I am happy to read that it help.

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
Reply
Loading...
Sep 16, 2020 0
Community Beginner ,
Sep 16, 2020

Copy link to clipboard

Copied

Oh, I didn't know there were those other places to add code. I'm brand new at this. 🙂

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
Reply
Loading...
Sep 16, 2020 0