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

# Rounding up

Community Beginner ,
Jun 28, 2020 Jun 28, 2020

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.

TOPICS
Acrobat SDK and JavaScript

Views

592

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 Expert , Jun 29, 2020 Jun 29, 2020

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);

6 Replies 6
Community Expert ,
Jun 28, 2020 Jun 28, 2020

Copied

You can specify this in the format of the field.

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 Expert ,
Jun 29, 2020 Jun 29, 2020

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);

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 Expert ,
Jun 29, 2020 Jun 29, 2020

Copied

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

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

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?

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 Expert ,
Jun 29, 2020 Jun 29, 2020

Copied

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

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

Copied

LATEST

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.