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

Acrobat Javascript Rechenfehler

Community Beginner ,
Mar 13, 2019 Mar 13, 2019

Copy link to clipboard

Copied

Hallo,

schwierig zu beschreiben aber vielleicht kennt jemand die Problematik zu nachstehenden vermeintlichen Rechenfehler in javascript das in einem PDF Formular eingesetzt wird.

Hier eine beispielhafte Rechnung:

1,15 - (1,15 * 0,05)

ergibt per Hand gerechnet eigentlich

1,0925

gebe ich diese Formel jedoch wie folgt als Berechnung in Acrobat ein

event.value =  1.15-(1.15*0.05)

ergibt diese Rechnung

1.0924999999999998

Frage:

Wieso werden hier Nachkommastellen eingefügt?

Wo ist hier mein Denkfehler?

Mein Problem ist, das sich dadurch Rundungsfehler im weiteren Verlauf der Berechnungen ergeben.

TOPICS
Acrobat SDK and JavaScript , Windows

Views

330

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 ,
Mar 13, 2019 Mar 13, 2019

Copy link to clipboard

Copied

This is a complex issue relating to how computer languages (and computers in general) represent floating numbers.

You can read about it more in depth here: What Every JavaScript Developer Should Know About Floating Points - Modern Web

The solution is to manually round the results to a fixed number of decimals.

You can use the toFixed method for that (although note that it returns a string, not a number), or write a function that does it using the various built-in commands of the Math object (ceil, floor, round).

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 ,
Mar 13, 2019 Mar 13, 2019

Copy link to clipboard

Copied

thank you for the really quick answer.

my problem with round is, that my client compares my calculation with excel . The calculation they do is simply using the excel-function to round-down to the 4th dezimal.

first example:

1.325-(1.325*0.05) = 1.256375 (result excel)

1.325-(1.325*0.05) = 1.256375 (same in javascript)

excel round down is: 1.2563

javascript would do the same: 1.2563

every thing is fine!

second example:

1.15-(1.15*0.05) = 1.0925  (result excel)

1.15-(1.15*0.05) =1.0924999999999998 (result in javascript)

excel round down is: 1.0925

javascript: 1.0924

And i have no clue how to fix that.

Do you?

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 ,
Mar 13, 2019 Mar 13, 2019

Copy link to clipboard

Copied

For example:

(1.15-(1.15*0.05)).toFixed(4)

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 ,
Mar 13, 2019 Mar 13, 2019

Copy link to clipboard

Copied

yup, the "funny" thing is:

(1.15-(1.15*0.05)).toFixed(4) = 1.0925

but now the result from the first example would be different from the excel one.

(1.3225-(1.3225*0.05)).toFixed(4) = 1.2564

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 ,
Mar 13, 2019 Mar 13, 2019

Copy link to clipboard

Copied

Yeah, that's annoying... So is Excel always rounding down? If so you'll need to write a custom function that checks the difference between your result and the nearest integer and then define a threshold to use between rounding up (in case of a floating number precision issue) or rounding down (in all other cases).

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 ,
Mar 13, 2019 Mar 13, 2019

Copy link to clipboard

Copied

thank you for your help!

i´ll have to read a bit more about this issue. As i know now, it is a really complex thing!

have a nice day!

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 ,
Mar 13, 2019 Mar 13, 2019

Copy link to clipboard

Copied

LATEST

If you're interested in hiring someone to do it for you feel free to contact me privately (try6767 at gmail.com). I've developed similar solution for my clients in the past.

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