Highlighted

Rounding up

Community Beginner ,
Jun 28, 2020

Copy link to clipboard

Copied

How can I roundup the answer to FieldA * FieldB to a product which is only 2 decimal points.

FieldB is a decimal number .001931

FieldA will always be a whole number with 2 decimal places.

I am trying to get if the answer is 5.235 to be 5.24 or if the answer is 5.231 to be 5.23

 

The calculation will be used in a fillable pdf.

 

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

That's a dangerous approach. It might look like the value has been rounded up, but in fact it won't be, and if you use it in another calculation you might not get the results you expect. It's better to actually change the actual value, like this:

 

 

var v1 = Number(this.getField("FieldA").valueAsString);
var v2 = Number(this.getField("FieldB").valueAsString);
var v3 = v1*v2;
event.value = v3.toFixed(2);

 

 

Topics

Acrobat SDK and JavaScript, Create PDFs, Edit and convert PDFs, PDF forms

Views

98

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 up

Community Beginner ,
Jun 28, 2020

Copy link to clipboard

Copied

How can I roundup the answer to FieldA * FieldB to a product which is only 2 decimal points.

FieldB is a decimal number .001931

FieldA will always be a whole number with 2 decimal places.

I am trying to get if the answer is 5.235 to be 5.24 or if the answer is 5.231 to be 5.23

 

The calculation will be used in a fillable pdf.

 

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

That's a dangerous approach. It might look like the value has been rounded up, but in fact it won't be, and if you use it in another calculation you might not get the results you expect. It's better to actually change the actual value, like this:

 

 

var v1 = Number(this.getField("FieldA").valueAsString);
var v2 = Number(this.getField("FieldB").valueAsString);
var v3 = v1*v2;
event.value = v3.toFixed(2);

 

 

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
Adobe Community Professional ,
Jun 28, 2020

Copy link to clipboard

Copied

You can specify this in the format of the field.

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...
Most Valuable Participant ,
Jun 29, 2020

Copy link to clipboard

Copied

That's a dangerous approach. It might look like the value has been rounded up, but in fact it won't be, and if you use it in another calculation you might not get the results you expect. It's better to actually change the actual value, like this:

 

 

var v1 = Number(this.getField("FieldA").valueAsString);
var v2 = Number(this.getField("FieldB").valueAsString);
var v3 = v1*v2;
event.value = v3.toFixed(2);

 

 

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...
Most Valuable Participant ,
Jun 29, 2020

Copy link to clipboard

Copied

I edited the code because I noticed that even though you asked for "rouding up" in your examples you actually used regular rounding...

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...
Community Beginner ,
Jun 29, 2020

Copy link to clipboard

Copied

Thank you, but I am a little ignorant on this. If I put this into the custom java script calculation box, how can I get the Field that I am entering the custom calculation  in to show the event.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...
Most Valuable Participant ,
Jun 29, 2020

Copy link to clipboard

Copied

Not sure what you mean... event.value is the new value that is assigned to the field.

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...
Community Beginner ,
Jun 29, 2020

Copy link to clipboard

Copied

Thank you but if I specify it in the format to 2 decimal places if the answer is 5.245 it will show on the form as 5.24 but will add as 5.25.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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...
Resources
One Stop Solution for Acrobat
Edit a PDF