Skip to main content
Participant
June 28, 2020
Answered

Rounding up

  • June 28, 2020
  • 1 reply
  • 998 views

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.

 

This topic has been closed for replies.
Correct answer try67

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

 

 

1 reply

Bernd Alheit
Community Expert
Community Expert
June 29, 2020

You can specify this in the format of the field.

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
June 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);

 

 

try67
Community Expert
Community Expert
June 29, 2020

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