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

Calculation with round up

Participant ,
Jun 14, 2019 Jun 14, 2019

hi, I'm using bellow script. my issue is if i do calculation separately with round up, result is not same. so i want edit my script get roundup total like bellow.

event.value = (this.getField("Tax").value / 100) * this.getField("M0").value;

event.value = (event.value <= 0)?"":event.value;

"Tax" field - 8.25%

"M0" = (J0 +K0 +L0)

so i want to do calculation like bellow.

(J0 * 8.25/100)  = Result round up to two decimal places

(K0 * 8.25/100)  = Result round up to two decimal places

(K0 * 8.25/100)  = Result round up to two decimal places

And get total off above all 3 values.

Hope my explanation is clear & some can help me. thanks..

TOPICS
Acrobat SDK and JavaScript , Windows
3.1K
Translate
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 14, 2019 Jun 14, 2019

None of the calculations you show will round up to two decimal places. If you display only two places, it will round in general (down or up), so in order to always round up, you will have to do an extra step. The following function will always round up to two decimal places:

function round_up(n) {

    return Math.ceil(n*100)/100;

}

With that, you can then calculate your total based on the three individual results. The only remaining question is if you want to round the numbers J0, K0 and M0 to the desired resolution, or if you want to round the sum of these numbers, or the sum of the calculated percentages.

Translate
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
Participant ,
Jun 14, 2019 Jun 14, 2019

hi, thanks a lot for your answer. i will explain my issue using two example values so u can get some idea.. pls help me get updated code. thanks again..

Tax = 8.25

J0 = 1191

K0 = 99

If we add above values then my script giving bellow answer

1194 + 99 = 1293 * 8.25% = 106.67

But i need it giving answer like bellow

1194 x 8.25 = 98.51

99 * 8.25% = 8.17

Total = 106.68

Translate
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 16, 2019 Jun 16, 2019

So, what you want is for the field values to be the actual calculated value, but the user display to show the rounded value.

This is the purpose of field formatting.

Do this:

1) Do not round any of the Calculation results. Make all the calculations return the natural result.

2) Set the field formatting to show two decimal places.

Done!

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
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
Participant ,
Jun 17, 2019 Jun 17, 2019

sorry it didn't work..

Translate
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 17, 2019 Jun 17, 2019

Use the function round_up

Translate
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
Participant ,
Jun 17, 2019 Jun 17, 2019

hi, thanks. is that possible to explain it little more ?? if u can give code s code that will be big help to me. thanks..

Translate
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
Participant ,
Jun 17, 2019 Jun 17, 2019
LATEST

hi, thanks. finally i have managed it. thanks..

var v1 =  Math.round(((this.getField("Tax").value / 100) * this.getField("J0").value)*100)/100; 

var v2 =  Math.round(((this.getField("Tax").value / 100) * this.getField("K0").value)*100)/100; 

var v3 =  Math.round(((this.getField("Tax").value / 100) * this.getField("L0").value)*100)/100;

event.value = v1 + v2 + v3;

event.value = (event.value <= 0)?"":event.value;

Translate
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