Gas Cost Calculator Formula

Understand the math behind the gas cost calculator. Each variable explained with a worked example.

Formulas Used

Total Fuel Cost

total_fuel_cost = gallons_needed * gas_price

Gallons Used

gallons_used = gallons_needed

Cost Per Mile

cost_per_mile = gas_price / mpg

Variables

VariableDescriptionDefault
trip_milesTrip Distance(miles)300
mpgVehicle MPG(MPG)28
gas_priceGas Price Per Gallon(USD/gal)3.5
gallons_neededDerived value= trip_miles / mpgcalculated

How It Works

How to Calculate Gas Cost

Divide the trip distance by your MPG to find gallons needed, then multiply by the gas price.

Formula

Gallons = Distance / MPG

Fuel Cost = Gallons x Price Per Gallon

Worked Example

300-mile trip in a car getting 28 MPG with gas at $3.50/gallon.

trip_miles = 300mpg = 28gas_price = 3.5
  1. 01Gallons needed = 300 / 28 = 10.71 gal
  2. 02Fuel cost = 10.71 x $3.50 = $37.50
  3. 03Cost per mile = $3.50 / 28 = $0.125

Ready to run the numbers?

Open Gas Cost Calculator