Math.floor does not work

New Here ,
Jan 06, 2018

Copy link to clipboard

Copied

I need help with this rounding:

var v3 = (this.getField("valor_for").value-10)/2;

event.value = Math.floor(v3);

I need the value of the operation always be rounded down

thanks

Adobe Community Professional
Correct answer by Karl Heinz Kremer | Adobe Community Professional

A good way to debug these things (besides actually running in the JavaScript debugger and stepping through your code one line at a time) is to add debug output to your script. Add these two lines just after you calculate v3:

console.println("v3 = " + v3);

console.println("Math.floor(v3) = " + Math.floor(v3));

Now you can see what your script does. It works correctly for me.

If you are not getting the correct results, and if the value of the "valor_for" field is calculated as well, you need to check your calculation order (which is what I think Bernd wanted to say): If the 2nd field gets calculated before the results of the first field are available, then you are working with the previous value, which could explain the behavior you are seeing.

TOPICS
Acrobat SDK and JavaScript, Windows

Views

217

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

Math.floor does not work

New Here ,
Jan 06, 2018

Copy link to clipboard

Copied

I need help with this rounding:

var v3 = (this.getField("valor_for").value-10)/2;

event.value = Math.floor(v3);

I need the value of the operation always be rounded down

thanks

Adobe Community Professional
Correct answer by Karl Heinz Kremer | Adobe Community Professional

A good way to debug these things (besides actually running in the JavaScript debugger and stepping through your code one line at a time) is to add debug output to your script. Add these two lines just after you calculate v3:

console.println("v3 = " + v3);

console.println("Math.floor(v3) = " + Math.floor(v3));

Now you can see what your script does. It works correctly for me.

If you are not getting the correct results, and if the value of the "valor_for" field is calculated as well, you need to check your calculation order (which is what I think Bernd wanted to say): If the 2nd field gets calculated before the results of the first field are available, then you are working with the previous value, which could explain the behavior you are seeing.

TOPICS
Acrobat SDK and JavaScript, Windows

Views

218

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
Jan 06, 2018 0
Adobe Community Professional ,
Jan 06, 2018

Copy link to clipboard

Copied

Please give some examples of when it’s not working correctly. What values do you enyet, what do you expect as output, and what is the actual output?

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...
Jan 06, 2018 0
New Here ,
Jan 06, 2018

Copy link to clipboard

Copied

When I write the code without Math.floor, the calculation works, for example:

var v3 = (this.getField("valor_for").value-10)/2;

event.value = (v3);

                                valor_for field                                          output of calculation (20-10)/2 = 5

fields_1.PNG

But when I put Math.floor, nothing happens:

var v3 = (this.getField("valor_for").value-10)/2;

event.value = Math.floor(v3);

                              valor_for field                                            output of calculation (31-10)/2 = 10,5 -

                                                                                          with Math.floor the result would be 10 right?

fields_2.PNG

these are my lines of code:

fields_3.PNG

What's wrong?

Thank you

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...
Jan 06, 2018 0
Adobe Community Professional ,
Jan 06, 2018

Copy link to clipboard

Copied

Check the calculation order.

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...
Jan 06, 2018 0
Adobe Community Professional ,
Jan 06, 2018

Copy link to clipboard

Copied

A good way to debug these things (besides actually running in the JavaScript debugger and stepping through your code one line at a time) is to add debug output to your script. Add these two lines just after you calculate v3:

console.println("v3 = " + v3);

console.println("Math.floor(v3) = " + Math.floor(v3));

Now you can see what your script does. It works correctly for me.

If you are not getting the correct results, and if the value of the "valor_for" field is calculated as well, you need to check your calculation order (which is what I think Bernd wanted to say): If the 2nd field gets calculated before the results of the first field are available, then you are working with the previous value, which could explain the behavior you are seeing.

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...
Jan 06, 2018 0