{
  "schemaVersion": 1,
  "slug": "fuel-economy-mpg-calculator",
  "category": "conversion",
  "kind": "implementation",
  "independentValidation": false,
  "implementationHash": "a4723fb419b10642ef58c67c4a2a8ad788d8725414bcf24a0141c51951956e51",
  "provenance": "Generated from the calculation contract used by the published page. Publication is not external validation.",
  "sourceFiles": [
    "data/calculators/fuel-economy-mpg-calculator.json",
    "src/engine.js"
  ],
  "runtimeSource": "/sources/engine.txt",
  "runtimeHash": "5c888d8898afbdee36267bb9977829e1e2ac568f6677c8ee87469c139864645c",
  "scope": "The compute function records implemented equations, constants, lookup tables and custom rules. Inputs record defaults and allowed values; outputs record precision and units. Defaults are examples, not recommendations. General background references are not evidence for this specific algorithm.",
  "publishedFormula": {
    "formula": "L/100 km = 235.2145833333333/(US mpg) = 282.48093633182214/(UK mpg) = 100/(km/L); litres = distance_km × (L/100 km)/100",
    "vars": [],
    "note": "Enable the optional journey to estimate litres from distance in kilometres and multiply by fuel price per litre. Journey cost uses the entered currency; no exchange conversion or real-world driving adjustment is applied."
  },
  "externalEvidence": {
    "reviewedAt": "2026-10-31",
    "sources": [
      {
        "name": "US DOE/EPA — Fuel economy calculations and trip cost inputs",
        "url": "https://www.fueleconomy.gov/feg/savemoney.shtml"
      }
    ],
    "referenceRole": "input",
    "implementationHash": "a4723fb419b10642ef58c67c4a2a8ad788d8725414bcf24a0141c51951956e51",
    "basis": "Convert between US mpg, Imperial mpg, L/100 km and km/L using the same reciprocal factors in both entries. Equal source and target units return the entered value. Enable the optional journey to estimate litres from distance in kilometres and multiply by fuel price per litre. Journey cost uses the entered currency; no exchange conversion or real-world driving adjustment is applied.",
    "limitations": "Enable the optional journey to estimate litres from distance in kilometres and multiply by fuel price per litre. Journey cost uses the entered currency; no exchange conversion or real-world driving adjustment is applied."
  },
  "contract": {
    "compute": "try{if(((true)&&(!Number.isFinite(value)||value<0.01||value>10000))||(([\"on\"].includes(String(journeyMode)))&&(!Number.isFinite(distance)||distance<0||distance>100000))||(([\"on\"].includes(String(journeyMode)))&&(!Number.isFinite(fuelPrice)||fuelPrice<0||fuelPrice>100)))throw new Error('inputError');const calculate=function(){const h=(function context() {\n  const DAY=86400000;\n  const own=(o,k)=>Object.prototype.hasOwnProperty.call(o,k);\n  const fail=()=>{throw new Error('inputError');};\n  const num=(v,min=-Infinity,max=Infinity,integer=false)=>{\n    if(!Number.isFinite(v)||v<min||v>max||(integer&&!Number.isInteger(v)))fail();return v;\n  };\n  const choice=(v,values)=>{if(!values.includes(v))fail();return v;};\n  const date=s=>{if(typeof s!=='string'||!/^\\d{4}-\\d{2}-\\d{2}$/.test(s)||s.startsWith('0000'))fail();const d=new Date(s+'T00:00:00Z');if(!Number.isFinite(+d)||d.toISOString().slice(0,10)!==s)fail();return d;};\n  const iso=d=>{if(!Number.isFinite(+d)||d.getUTCFullYear()<1||d.getUTCFullYear()>9999)fail();return d.toISOString().slice(0,10);};\n  const shift=(d,n)=>{const r=new Date(+d+n*DAY);iso(r);return r;};\n  const month=(d,n)=>{const r=new Date(+d);r.setUTCDate(1);r.setUTCMonth(r.getUTCMonth()+n);const end=new Date(+r);end.setUTCMonth(end.getUTCMonth()+1,0);r.setUTCDate(Math.min(d.getUTCDate(),end.getUTCDate()));iso(r);return r;};\n  const weekdays=['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];\n  const work=(d,count)=>{let n=Math.floor(count/7)*5;for(let k=0;k<count%7;k++){const w=(d.getUTCDay()+k)%7;if(w!==0&&w!==6)n++;}return n;};\n  const wholeMonths=(a,b)=>{let n=(b.getUTCFullYear()-a.getUTCFullYear())*12+b.getUTCMonth()-a.getUTCMonth();if(n>=0&&+month(a,n)>+b)n--;if(n<0&&+month(a,n)<+b)n++;return n;};\n  return {DAY,own,fail,num,choice,date,iso,shift,month,weekdays,work,wholeMonths};\n})();const result=(function(v,h){\n    const {num,choice}=h,U={l100:'L/100 km',mpgus:'US mpg',mpguk:'UK mpg',kmpl:'km/L'},R={mpgus:235.2145833333333,mpguk:282.48093633182214,kmpl:100};num(v.value,0.01,10000);choice(v.from,Object.keys(U));choice(v.to,Object.keys(U));choice(v.journeyMode,['off','on']);\n    const l100=v.from==='l100'?v.value:R[v.from]/v.value;let litresUsed=null,journeyCost=null;if(v.journeyMode==='on'){num(v.fuelPrice,0,100);num(v.distance,0,100000);litresUsed=l100*v.distance/100;journeyCost=litresUsed*v.fuelPrice;}\n    return {result:v.from===v.to?v.value:v.to==='l100'?l100:R[v.to]/l100,mpgus:R.mpgus/l100,mpguk:R.mpguk/l100,kmpl:100/l100,l100,litresUsed,journeyCost,__resultUnit:U[v.to]};\n  })({\"value\":(value),\"from\":(from),\"to\":(to),\"journeyMode\":(journeyMode),\"distance\":(distance),\"fuelPrice\":(fuelPrice)},h);return result;};const out=calculate();return Object.fromEntries([\"result\",\"mpgus\",\"mpguk\",\"l100\",\"kmpl\",\"litresUsed\",\"journeyCost\"].map(k=>[k,out[k]??null]).concat(Object.entries(out).filter(([k])=>k.startsWith('__'))));}catch(error){return Object.assign(Object.fromEntries([\"result\",\"mpgus\",\"mpguk\",\"l100\",\"kmpl\",\"litresUsed\",\"journeyCost\"].map(k=>[k,null])),{__error:error.message==='openingsExceedWallArea'?'openingsExceedWallArea':'inputError'});}",
    "inputs": [
      {
        "name": "value",
        "type": "number",
        "default": 30,
        "min": 0.01,
        "max": 10000,
        "step": 0.1,
        "requiredValue": true,
        "strictRange": true
      },
      {
        "name": "from",
        "type": "select",
        "default": "mpgus",
        "options": [
          {
            "value": "mpgus"
          },
          {
            "value": "l100"
          },
          {
            "value": "mpguk"
          },
          {
            "value": "kmpl"
          }
        ]
      },
      {
        "name": "to",
        "type": "select",
        "default": "l100",
        "options": [
          {
            "value": "l100"
          },
          {
            "value": "mpgus"
          },
          {
            "value": "mpguk"
          },
          {
            "value": "kmpl"
          }
        ]
      },
      {
        "name": "precision",
        "type": "select",
        "default": 4,
        "options": [
          {
            "value": 0
          },
          {
            "value": 2
          },
          {
            "value": 4
          },
          {
            "value": 6
          }
        ]
      },
      {
        "type": "select",
        "default": "off",
        "options": [
          {
            "value": "off"
          },
          {
            "value": "on"
          }
        ],
        "advanced": true,
        "name": "journeyMode"
      },
      {
        "type": "number",
        "default": 100,
        "min": 0,
        "max": 100000,
        "step": 0.01,
        "strictRange": true,
        "requiredValue": true,
        "advanced": true,
        "name": "distance",
        "showWhen": {
          "journeyMode": [
            "on"
          ]
        }
      },
      {
        "type": "number",
        "default": 1.5,
        "min": 0,
        "max": 100,
        "step": 0.01,
        "strictRange": true,
        "requiredValue": true,
        "advanced": true,
        "name": "fuelPrice",
        "showWhen": {
          "journeyMode": [
            "on"
          ]
        }
      }
    ],
    "outputs": [
      {
        "name": "result",
        "precision": 4,
        "precisionField": "precision",
        "unitField": "__resultUnit",
        "trimTrailingZeros": true,
        "notation": "auto"
      },
      {
        "name": "mpgus",
        "precision": 2,
        "unit": "mpg",
        "notation": "auto",
        "showWhen": {
          "to": [
            "l100",
            "mpguk",
            "kmpl"
          ]
        }
      },
      {
        "name": "mpguk",
        "precision": 2,
        "unit": "mpg",
        "notation": "auto",
        "showWhen": {
          "to": [
            "l100",
            "mpgus",
            "kmpl"
          ]
        }
      },
      {
        "name": "l100",
        "precision": 2,
        "unit": "L/100km",
        "notation": "auto",
        "showWhen": {
          "to": [
            "mpgus",
            "mpguk",
            "kmpl"
          ]
        }
      },
      {
        "name": "kmpl",
        "precision": 3,
        "unit": "km/L",
        "showWhen": {
          "to": [
            "l100",
            "mpgus",
            "mpguk"
          ]
        },
        "notation": "auto"
      },
      {
        "name": "litresUsed",
        "precision": 2,
        "unit": "L",
        "notation": "auto",
        "optional": true
      },
      {
        "name": "journeyCost",
        "precision": 2,
        "unit": "$",
        "notation": "auto",
        "optional": true
      }
    ],
    "interpret": null
  }
}
