{
  "id": "batting-average",
  "version": "cab427588b25",
  "status": "published",
  "name": "Batting Average Calculator",
  "question": "What is my batting average?",
  "summary": "Computes a batting average (hits ÷ at-bats) as Official Baseball Rule 9.21 defines it, with on-base percentage, slugging percentage and OPS when you add walks, hit by pitches, sacrifice flies and extra-base hits.",
  "category": "sports",
  "subcategory": "scoring",
  "url": "https://www.acalculator.org/sports/batting-average-calculator",
  "markdown": "https://www.acalculator.org/sports/batting-average-calculator.md",
  "kind": "function",
  "method": "AVG = H ÷ AB; OBP = (H + BB + HBP) ÷ (AB + BB + HBP + SF); SLG = TB ÷ AB with TB = H + 2B + 2 × 3B + 3 × HR; OPS = OBP + SLG.",
  "assumptions": [
    "At-bats are official at-bats (Rule 9.02(a)): walks, hit by pitches, sacrifice bunts and flies, and catcher’s interference do not count.",
    "Empty walks leave out OBP and OPS; empty doubles, triples and home runs leave out slugging and OPS. Inside each, an empty count is 0."
  ],
  "inputs": {
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "type": "object",
    "properties": {
      "h": {
        "title": "Hits (H)",
        "description": "Safe hits of any kind: singles, doubles, triples and home runs.",
        "type": "integer",
        "minimum": 0,
        "maximum": 100000
      },
      "ab": {
        "title": "At-bats (AB)",
        "description": "Official times at bat: plate appearances minus walks, hit by pitches, sacrifices and catcher’s interference.",
        "type": "integer",
        "minimum": 1,
        "maximum": 100000
      },
      "bb": {
        "title": "Walks (BB)",
        "description": "Bases on balls, intentional ones included. Leave empty to skip OBP.",
        "type": "integer",
        "minimum": 0,
        "maximum": 100000
      },
      "hbp": {
        "title": "Hit by pitch (HBP)",
        "description": "Times hit by a pitch. Empty counts as 0.",
        "type": "integer",
        "minimum": 0,
        "maximum": 100000
      },
      "sf": {
        "title": "Sacrifice flies (SF)",
        "description": "Sacrifice flies. Empty counts as 0.",
        "type": "integer",
        "minimum": 0,
        "maximum": 100000
      },
      "d": {
        "title": "Doubles (2B)",
        "description": "Two-base hits. Fill in any of 2B, 3B or HR to get slugging and OPS.",
        "type": "integer",
        "minimum": 0,
        "maximum": 100000
      },
      "t": {
        "title": "Triples (3B)",
        "description": "Three-base hits. Empty counts as 0.",
        "type": "integer",
        "minimum": 0,
        "maximum": 100000
      },
      "hr": {
        "title": "Home runs (HR)",
        "description": "Home runs. Empty counts as 0.",
        "type": "integer",
        "minimum": 0,
        "maximum": 100000
      }
    }
  },
  "outputs": {
    "average": {
      "label": "Batting average (AVG)",
      "description": "Hits ÷ at-bats.",
      "format": "number"
    },
    "display": {
      "label": "Batting average",
      "description": "The batting average to three places, as box scores write it.",
      "format": "text"
    },
    "obp": {
      "label": "On-base percentage (OBP)",
      "description": "(H + BB + HBP) ÷ (AB + BB + HBP + SF).",
      "format": "number"
    },
    "slg": {
      "label": "Slugging percentage (SLG)",
      "description": "Total bases ÷ at-bats.",
      "format": "number"
    },
    "ops": {
      "label": "OPS",
      "description": "On-base percentage + slugging percentage.",
      "format": "number"
    },
    "totalBases": {
      "label": "Total bases (TB)",
      "description": "H + 2B + 2 × 3B + 3 × HR.",
      "format": "integer"
    }
  },
  "defaultAnswer": {
    "inputs": {
      "h": 165,
      "ab": 440
    },
    "outputs": {
      "average": 0.375,
      "display": ".375"
    },
    "text": "165 hits in 440 at-bats is a batting average of .375."
  },
  "examples": [
    {
      "given": {
        "h": 165,
        "ab": 440
      },
      "expect": {
        "average": 0.375,
        "display": ".375"
      },
      "source": "Official Baseball Rules (2025), Rule 9.22(a) Minimum Standards for Individual Championships, example (Baker, .375), https://baseballrulesacademy.com/official-rule/mlb/9-22-minimum-standards-individual-championships/; hand calculation in content.mdx: 165 ÷ 440 = 0.375"
    },
    {
      "given": {
        "h": 165,
        "ab": 452
      },
      "expect": {
        "average": 0.36504424778761063,
        "display": ".365"
      },
      "source": "Official Baseball Rules (2025), Rule 9.22(a), example (.365 with 12 more at-bats), https://baseballrulesacademy.com/official-rule/mlb/9-22-minimum-standards-individual-championships/; hand calculation in content.mdx: 165 ÷ 452 = 0.36504"
    },
    {
      "given": {
        "h": 150,
        "ab": 500,
        "bb": 60,
        "hbp": 5,
        "sf": 5,
        "d": 30,
        "t": 2,
        "hr": 25
      },
      "expect": {
        "average": 0.3,
        "display": ".300",
        "obp": 0.37719298245614036,
        "totalBases": 259,
        "slg": 0.518,
        "ops": 0.8951929824561404
      },
      "source": "Official Baseball Rules (2025), Rule 9.21 Determining Percentage Records, https://baseballrulesacademy.com/official-rule/mlb/9-21-determining-percentage-records/; hand calculation in content.mdx: OBP = 215 ÷ 570, TB = 150 + 30 + 4 + 75 = 259, SLG = 259 ÷ 500",
      "tolerance": 1e-15
    },
    {
      "given": {
        "h": 3,
        "ab": 16
      },
      "expect": {
        "average": 0.1875,
        "display": ".188"
      },
      "source": "Official Baseball Rules (2025), Rule 9.21 Determining Percentage Records, https://baseballrulesacademy.com/official-rule/mlb/9-21-determining-percentage-records/; hand calculation in content.mdx: 3 ÷ 16 = 0.1875, written .188"
    }
  ],
  "sources": [
    "Official Baseball Rules (2025), Rule 9.21 Determining Percentage Records: batting average, slugging percentage and on-base percentage. https://baseballrulesacademy.com/official-rule/mlb/9-21-determining-percentage-records/ (retrieved 2026-10-01)",
    "Official Baseball Rules (2025), Rule 9.22(a) Minimum Standards for Individual Championships, with the Baker example. https://baseballrulesacademy.com/official-rule/mlb/9-22-minimum-standards-individual-championships/ (retrieved 2026-10-01)"
  ],
  "related": [
    "era",
    "win-percentage"
  ],
  "changelog": []
}
