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?
+++ 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
...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?
Copy link to clipboard
Copied
For the calculation use the method toFixed.
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.
Copy link to clipboard
Copied
Code is working from what I can see. what is the problem?
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));
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:
Copy link to clipboard
Copied
Thank you, everyone. This really helped!
Copy link to clipboard
Copied
Working with money needs a lot of care, it has more complications than people expect. In particular you need to round EACH line item that is added up, so that the displayed sum matches what a human checker would get. You can't just round the total, which is the obvious thing to do.
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.
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).
event.value = ( (0.575)+1.7).toFixed(1);
Why?
To explain this better in a more visual way see the next slide below:
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
Copy link to clipboard
Copied
3*0.575=1.725 toFixed(2) doesn't roundup 5 so it stays as 1.72 on the other hand format as number with 3 decimals is also
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.
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.
Copy link to clipboard
Copied
I like more Bernd Alheit's method better applying the Math.round.
This works perfect for my forms.
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:
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...
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: