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

Rounding up

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

 

TOPICS
Acrobat SDK and JavaScript

Views

519

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

correct answers 1 Correct answer

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

 

 

Votes

Translate

Translate
Community Expert ,
Jun 28, 2020 Jun 28, 2020

Copy link to clipboard

Copied

You can specify this in the format of the field.

Votes

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 29, 2020 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);

 

 

Votes

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

Votes

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

 

Votes

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

Votes

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

Copy link to clipboard

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.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Votes

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