Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Exit

- Home
- Acrobat SDK
- Discussions
- Help with Summing a rounded Calculation

- Help with Summing a rounded Calculation

0

/t5/acrobat-sdk-discussions/help-with-summing-a-rounded-calculation/td-p/11354312
Aug 11, 2020
Aug 11, 2020

Copy link to clipboard

Copied

Perhaps it is because I am overthinking this. I am creating a form where someone is reimbursed for mileage at the government rate of $0.575 (mileage rate). I created a cell where it would calculate the mileage in USD based on the number of miles someone drove (mileage=miles x rate). But at the end of the row it is supposed to total the mileage of 7 days.

So if someone drove 1 mile each day it is showing as each days as $0.58 miles. Which is correct, but the total at the end of the row is $4.03 (which is $0.575*7=$4.025). But I want the total to show as $4.06 ($0.58 rounded product x 7 days). Can someone help me?

TOPICS

Acrobat SDK and JavaScript

Community guidelines

Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

Community Expert
,
Aug 23, 2020
Aug 23, 2020

+++ UPDATE

Below these link from the IRS are the scripts that you must use to calculate the appropriate gas mileage rates in accordance with the IRS standard mileage rates for fiscal year (FY) 2020:

Right off the bat... the link above states the following:

"*IR-2019-215, December 31, 2019*

*WASHINGTON — The Internal Revenue Service today issued the 2020 optional standard mileage rates (PDF) used to calculate the deductible cost*

15
Replies
15

/t5/acrobat-sdk-discussions/help-with-summing-a-rounded-calculation/m-p/11354411#M3210
Aug 11, 2020
Aug 11, 2020

Copy link to clipboard

Copied

Hi, can you explain what calculation you use to get 4.025?

can you share your file perhaps or tell us how many fields you have and how are they formatted?

Community guidelines

Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

/t5/acrobat-sdk-discussions/help-with-summing-a-rounded-calculation/m-p/11354590#M3429
Aug 12, 2020
Aug 12, 2020

Copy link to clipboard

Copied

For the calculation use the method toFixed.

Community guidelines

Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

Lily5EDC
AUTHOR

Community Beginner
,

/t5/acrobat-sdk-discussions/help-with-summing-a-rounded-calculation/m-p/11360029#M3431
Aug 14, 2020
Aug 14, 2020

Copy link to clipboard

Copied

How would I code this?

var a=this.getField("MileageRate");

var b=this.getField("Miles.0");

var c=this.getField("Mileage.0");

c.value=(a.value*b.value);

(c.value.toFixed(2));

Or is there a better code than this? If so could you help? I need this for a form and I don't know anything about this.

Community guidelines

/t5/acrobat-sdk-discussions/help-with-summing-a-rounded-calculation/m-p/11360054#M3432
Aug 14, 2020
Aug 14, 2020

Copy link to clipboard

Copied

Code is working from what I can see. what is the problem?

Community guidelines

/t5/acrobat-sdk-discussions/help-with-summing-a-rounded-calculation/m-p/11360086#M3433
Aug 14, 2020
Aug 14, 2020

Copy link to clipboard

Copied

Use this:

```
var a=this.getField("MileageRate");
var b=this.getField("Miles.0");
var c=this.getField("Mileage.0");
c.value=Number((a.value*b.value).toFixed(2));
```

Community guidelines

/t5/acrobat-sdk-discussions/help-with-summing-a-rounded-calculation/m-p/11361052#M3434
Aug 14, 2020
Aug 14, 2020

Copy link to clipboard

Copied

What they meant is that they wrote the code for you .

All you have to do is copy the code from here (in this webchat) and paste that code in custom calculation script section of the field that will output the rounded total.

See the slide below:

Community guidelines

Lily5EDC
AUTHOR

Community Beginner
,

/t5/acrobat-sdk-discussions/help-with-summing-a-rounded-calculation/m-p/11375163#M3435
Aug 20, 2020
Aug 20, 2020

Copy link to clipboard

Copied

Thank you, everyone. This really helped!

Community guidelines

/t5/acrobat-sdk-discussions/help-with-summing-a-rounded-calculation/m-p/11375197#M3436
Aug 20, 2020
Aug 20, 2020

Copy link to clipboard

Copied

Community guidelines

Lily5EDC
AUTHOR

Community Beginner
,

/t5/acrobat-sdk-discussions/help-with-summing-a-rounded-calculation/m-p/11375695#M3437
Aug 20, 2020
Aug 20, 2020

Copy link to clipboard

Copied

Thank you, Test Screen Name. When I used the toFixed Code, the mileage rate of $0.575 won't round up the numbers before cutting it to the tenth place.

For example 3 miles at $0.575, instead of being $1.73 ($1.725 rounded up) comes out as $1.72.

And I have no idea what is going on with the sum. 0.57+1.72 is $2.30 not $2.29.

Community guidelines

/t5/acrobat-sdk-discussions/help-with-summing-a-rounded-calculation/m-p/11375772#M3438
Aug 20, 2020
Aug 20, 2020

Copy link to clipboard

Copied

+++EDITED REPLY , fixed errors in my suggested guidance

++Adding to the discussion,

You're missing a few details.

If you've used the toFixed method for every numeric field in your PDF, don't do that. Only use this method in the grand total field(s).

- First off, you're not adding .57 the real value that you should be using is .575.

- The rounding up will be handled automatically because of the built-in basic arithmetic rules . To fix this, you must follow the steps below.

- Second, you need to format the the total field with Acrobat's built-in formats. See slide below and set up tthe grand total field exactly as shown. Note how I 've selected 2 decimal places. When you set this up to two decimal places to calculate a number with 3 decimal places to the right of the decimal period (".") it will round up automatically.

- Last, your script should be phrased like this:

`event.value = ( (0.575)+1.7).toFixed(1);`

Why?

- Because you have to follow the basic "6th grader" math rules for rounding up or down.

- And because sometimes, JavaScript handles arithmetic with numbers totally different.

- Anything greater than 5 to the right of the decimal (or to the tenth place) rounds up the nearest integer number to its left by adding a "1". If it is less than 5 it rounds down the nearest integer number to the right.

- So if you apply a
**toFixed(2)**method you would get an incorrect total because you may end up running into a double rounding issue which is not what you want.

To explain this better in a more visual way see the next slide below:

Community guidelines

/t5/acrobat-sdk-discussions/help-with-summing-a-rounded-calculation/m-p/11375984#M3439
Aug 20, 2020
Aug 20, 2020

Copy link to clipboard

Copied

In Acrobat the calculation of 0.575 * 3 gives as result 1.7249999999999999

(0.575 * 3).toFixed(2) delivers 1.72

Math.round(0.575 * 3 * 100)/100 gives you 1.73

Community guidelines

/t5/acrobat-sdk-discussions/help-with-summing-a-rounded-calculation/m-p/11376046#M3440
Aug 21, 2020
Aug 21, 2020

Copy link to clipboard

Copied

1.725 but with 2 decimals it round it up to 1.73.Correct answer should be 1.725 and it shouldn't be rounded or showed as 2 decimals

because if used toFixed(2) 1.72+1.72=3.44 and if used as format number with 2 decimals it's 1.73+1.73=3.46 and correct answer is

1.725+1.725=3.45 .

Rounding is easier to use but it doesn't give accurate result, so avoid rounding if you want more accurate result. In your case don't use toFixed and don't use format as number and you will get accurate result which is

3*0.575=1.725.

Community guidelines

/t5/acrobat-sdk-discussions/help-with-summing-a-rounded-calculation/m-p/11377486#M3441
Aug 21, 2020
Aug 21, 2020

Copy link to clipboard

Copied

That is right.

I don't understand why the gas mileage rate needs to be rounded up in a governement form.

Lily5EDC:

The user should only obtain a round up of the total, not the calculating values.

You never do that.

Besides if this procedure was taking from a government form that is supposed to be filled in manually you should read the instructions.

EVERY government form has instruction on how the form should be filled in, either a regulation, a policy, or a pamhlet.

This is exactly how corrupt banks, insurance companies, and governments make millions of dollars

And this is exactly how Family courts end up accusing parents of coming short with their monthly child-support payments ; so about 100 to 300 child support parents go to jail every day in the United States.

Is little stupid intentional "glitches" like this in software that charges tax payers and customers less than a fraction of a cent from their daily transactions.

And because is less than a fraction of a cent per transaction, our brain is pre-conditioned to accept that that this OK... It doesn't seem a lot ...right?

So, Nobody really gives a rat's butt. But you should because as you mentioned, this is for government gas mileage reimbursement.

When you multiply that fraction of a cent by all the people that are charged with in a country (or government personnel who claim gas mileage reimbursement in this case), by all the transcations that an individual do per day, per week, per month, per year, then you're talking about millions of dollars charged to the people or paid to the claiming individuals incorrectly.

Your PDF could end up overcharging gas mileage to government amd reimbursing government personnel with more than they should; that is consider defraud to the Government.

Don't use toFixed with calculating values, just use it in the Grand Total.

Community guidelines

/t5/acrobat-sdk-discussions/help-with-summing-a-rounded-calculation/m-p/11378867#M3442
Aug 22, 2020
Aug 22, 2020

Copy link to clipboard

Copied

I like more Bernd Alheit's method better applying the Math.round.

This works perfect for my forms.

Community guidelines

Community Expert
,

LATEST
/t5/acrobat-sdk-discussions/help-with-summing-a-rounded-calculation/m-p/11380368#M3443
Aug 23, 2020
Aug 23, 2020

Copy link to clipboard

Copied

+++ UPDATE

Below these link from the IRS are the scripts that you must use to calculate the appropriate gas mileage rates in accordance with the IRS standard mileage rates for fiscal year (FY) 2020:

Right off the bat... the link above states the following:

"*IR-2019-215, December 31, 2019*

*WASHINGTON — The Internal Revenue Service today issued the 2020 optional standard mileage rates (PDF) used to calculate the deductible costs of operating an automobile for business, charitable, medical or moving purposes.*

*Beginning on January 1, 2020, the standard mileage rates for the use of a car (also vans, pickups or panel trucks) will be:*

**57.5 cents per mile driven for business use, down one half of a cent from the rate for 2019,**

*17 cents per mile driven for medical or moving purposes, down three cents from the rate for 2019, and*

*14 cents per mile driven in service of charitable organizations.*

**The business mileage rate decreased one half of a cent for business travel driven and three cents for medical and certain moving expense from the rates for 2019.** The charitable rate is set by statute and remains unchanged."

You've been using dollar value to work around the calculation when in fact, the mileage rate per miles driven in a day is calulated by multiplying total miles driven by 57.5 cents per mile driven

In FY2019 it used used to be 58 cents per mile driven.

So, your calculation should be performed with cents per mile ( 57.5) not cents converted to dollars (.575) to begin with.

Here's a couple of my scripts that works around this almost 98.9999 accurately:

```
//using 57.7 cents value following IRS guidance with util.printf() method for rounding
event.value = util.printf("$%,0.2f", ((this.getField("Mileage.0").value * 57.5) * .01));
//or you can also work it out like this with the multply by 100 divide by hundred to handle more complex rounding if the rate changes to something like "68.9485" cents
event.value = util.printf("$%,0.2f", (((this.getField("Mileage.0").value * 57.5) * .01)*100)/100);
//using dollar value $ 0.575 -->> Bernd_Alheit method with Math.round() function for rounding is not wrong but causes incorrect rounding because is using dollar value
event.value = Math.round ((this.getField("Mileage.0").value * .575)*100)/100;
```

If at some point in the near future you take some time to understand what the toFixed function does in JavaScript, you won't use it to round numbers.

This is where the problem is and how JavaScript is taught to people in general. The term "rounding" a number is used interchangeably with this function when it shouldn't.

toFixed basically cuts a string chunk out of a number that have decimal points, it doesn't really round nothing unless you apply a bunch of "Band-Aid" javascripting remedies in an attempt to force the output of a desired number value, not to mention other issues that we frequently run into in regards of floating points.

As a fixed point notation function, toFixed is rather a truncating formatting function (in my humble opinion). And wether it allows to manipulate the output string value of a number or not, you still have to decide how many digits need to be cut off to the right of the decimal point.

This is oviously something you don't want to do ina PDF government form to repair money reimbursement calculated values.

The truth is that , in real math the term "rounding" doesn't carry the same definition as toFixed.

To really round a number you need to use other functions or a combination of them, like for example, the Math.round() function (as illustrated by Bernd_Alheit).

In addition, See the quote below, teaken from this brief tutorial: https://medium.com/swlh/how-to-round-to-a-certain-number-of-decimal-places-in-javascript-ed74c471c1b...

**"if you want to round 0.2345 to two decimal places, you need to round 23.45 (0.2345*100), then divide the result (23) by 100 to get 0.23."**

And like Test_Screen_Name indicated : "*Working with money needs a lot of care, it has more complications than people expect.*"

In any case, today's your lucky day!! And please excuse my very long reply. I just want to make sure that you get this done correctly.

So, I prepared a PDF just for you with the scripts in it so you can use them and apply them: GAS MILEAGE RATE CALCULATION

Last, I will not spam any more this discussion but I would like to close my argument with the following slide:

Community guidelines

Copyright © 2023 Adobe. All rights reserved.