Skip to main content
Participant
May 13, 2025 10:50 AM UTC
Question

Rounding

Dear community,

I am Marcel from Germany. I have been unsuccessfully trying to round a given number (e.g., 5.25) to two decimal places in a commercial manner (rounding up with 5, 6, 7, 8, 9 and rounding down with 1, 2, 3, 4). The result should be 5.30, but with JavaScript, I always get 5.20, which is incorrect. Unfortunately, I cannot find an option in Acrobat Pro DC to solve this common problem. Neither Google nor ChatGPT have provided a solution. Can anyone help me?

Thank you!
Marcel

5 replies

try67
Community Expert
Community Expert
May 13, 2025 10:57 PM UTC

As the custom Calculation script of the field (NOT Format!), use this code:

if (event.value) event.value = Number(event.value).toFixed(2);

Translate
Participant
May 14, 2025 7:50 AM UTC

Thanks for your answers =)))

At the moment I have this one:

(function() {

var hin = parseFloat(this.getField("Km HinfahrtFahrt mit dem privaten Pkw")
.valueAsString.replace(",", ".")) || 0;
var rueck = parseFloat(this.getField("Km RückfahrtFahrt mit dem privaten Pkw")
.valueAsString.replace(",", ".")) || 0;


var kosten = (hin + rueck) * 0.35;


var gerundet = Math.round(kosten * 100) / 100;


event.value = gerundet.toFixed(2);
})();

 

Test: 1,5*0,35, he calculates 0,52 instead of 0,53..

Do you have an idea? I'm so confused that a simple thing like rounding is such a problem in this software 😞

Translate
try67
Community Expert
Community Expert
May 14, 2025 7:59 AM UTC

Rounding is actually a pretty challenging technical issue, related to the difficulty of floating point precision.

So the results of (1.5*0.35) in JS are actually 0.5249999999999999, which is why when you round it, it becomes 0.52, as it rounds down.

Translate
Bernd Alheit
Community Expert
Community Expert
May 13, 2025 12:04 PM UTC

What script do you use?

Translate