DEV Community

Tidy Cents
Tidy Cents

Posted on

IFTA fuel tax in Python: fleet MPG, taxable gallons and credits on one return

A common shortcut for the quarterly IFTA return is miles in a state times that state's rate. The return works differently:

  1. One fleet MPG: total miles everywhere / total gallons bought everywhere, rounded to 2 decimals.
  2. Taxable gallons per state come from where you drove: state miles / MPG, rounded to whole gallons.
  3. Net gallons = taxable gallons - gallons bought in that state. Negative means credit.
  4. Tax = net x the state's rate, and credits offset debts on the same return.
from decimal import Decimal as D, ROUND_HALF_UP

def ifta(lines, total_miles, total_gallons):
    """lines: {state: (taxable_miles, gallons_bought, rate)}"""
    mpg = (D(total_miles) / D(total_gallons)).quantize(D("0.01"), ROUND_HALF_UP)
    total = D(0)
    for st, (miles, bought, rate) in lines.items():
        taxable = (D(miles) / mpg).quantize(D("1"), ROUND_HALF_UP)   # where you DROVE
        net = taxable - bought                                         # negative = credit
        tax = net * D(rate)
        total += tax
        print(f"{st}: taxable {taxable}  net {net:+}  tax {tax:+.2f}")
    return mpg, total

mpg, total = ifta({"A": (2400, 300, "0.40"), "B": (1800, 400, "0.30"), "C": (1200, 150, "0.50")},
                  5400, 850)
print("fleet MPG", mpg, "| quarter", total)
Enter fullscreen mode Exit fullscreen mode

Output (illustrative rates):

A: taxable 378  net +78  tax +31.20
B: taxable 283  net -117  tax -35.10
C: taxable 189  net +39  tax +19.50
fleet MPG 6.35 | quarter 15.60
Enter fullscreen mode Exit fullscreen mode

Round the MPG first: it feeds every line, so rounding at the end gives different gallons. Some jurisdictions add a surcharge on full taxable gallons regardless of fuel bought; add it as a separate line, never folded into the rate. Real rates change every quarter.

Browser version with the full state list: IFTA Fuel Tax Calculator (free, no sign-up).

Top comments (0)