{
  "id": "decimal",
  "version": "a31773148820",
  "status": "published",
  "name": "Decimal Calculator",
  "question": "How do I calculate with decimals?",
  "summary": "Adds, subtracts, multiplies, or divides two decimals exactly, shows the working, and rounds the answer to any number of decimal places.",
  "category": "math",
  "subcategory": "arithmetic",
  "url": "https://www.acalculator.org/math/decimal-calculator",
  "markdown": "https://www.acalculator.org/math/decimal-calculator.md",
  "kind": "function",
  "method": "Add or subtract: write both numbers with the same number of decimal places and add the whole numbers. Multiply: multiply without the points; the product has as many decimal places as both numbers together. Divide: move both points right until both are whole, then divide.",
  "assumptions": [
    "Each number is read exactly as typed; there is no rounding in the answer.",
    "A quotient that does not end is written with a bar over the repeating digits (3.3̅ = 3.333…), or with its first 60 decimal places and \"…\" if no block repeats by then.",
    "The rounded answer keeps exactly the chosen number of decimal places; halves round away from zero.",
    "Dividing by 0 has no answer."
  ],
  "inputs": {
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "type": "object",
    "properties": {
      "a": {
        "title": "First number",
        "description": "A decimal number such as 12.5, -0.75 or 1,234.56.",
        "type": "string",
        "maxLength": 60
      },
      "op": {
        "title": "Operation",
        "description": "Add, subtract, multiply, or divide the first number by the second.",
        "type": "string",
        "enum": [
          "+",
          "-",
          "*",
          "/"
        ]
      },
      "b": {
        "title": "Second number",
        "description": "A decimal number such as 3.75, -2 or 0.005.",
        "type": "string",
        "maxLength": 60
      },
      "dp": {
        "title": "Round to (decimal places)",
        "description": "How many decimal places the rounded answer keeps, from 0 to 20.",
        "type": "integer",
        "minimum": 0,
        "maximum": 20
      }
    }
  },
  "outputs": {
    "result": {
      "label": "Answer",
      "description": "The exact answer: every digit when it ends, or the repeating digits marked with a bar.",
      "format": "text"
    },
    "rounded": {
      "label": "Rounded",
      "description": "The answer rounded to the chosen number of decimal places; halves round away from zero.",
      "format": "text"
    },
    "fraction": {
      "label": "As a fraction",
      "description": "The exact answer as a fraction in lowest terms.",
      "format": "text"
    },
    "steps": {
      "label": "Working",
      "description": "How the answer is found by hand, one step at a time.",
      "format": "text"
    },
    "problem": {
      "label": "Problem",
      "description": "The two numbers and the operation, written out in full.",
      "format": "text"
    }
  },
  "defaultAnswer": {
    "inputs": {
      "a": "12.5",
      "op": "+",
      "b": "3.75",
      "dp": 2
    },
    "outputs": {
      "result": "16.25",
      "rounded": "16.25",
      "fraction": "65/4",
      "steps": "Line up the decimal points with 2 decimal places each: 12.50 and 3.75; Add as whole numbers: 1250 + 375 = 1625; Put the decimal point back 2 places from the right: 16.25",
      "problem": "12.5 + 3.75"
    },
    "text": "12.5 + 3.75 = 16.25."
  },
  "examples": [
    {
      "given": {
        "a": "12.5",
        "op": "+",
        "b": "3.75",
        "dp": 1
      },
      "expect": {
        "result": "16.25",
        "rounded": "16.3",
        "fraction": "65/4",
        "steps": "Line up the decimal points with 2 decimal places each: 12.50 and 3.75; Add as whole numbers: 1250 + 375 = 1625; Put the decimal point back 2 places from the right: 16.25"
      },
      "source": "hand calculation in content.mdx; 16.25 is halfway, so it rounds away from zero to 16.3; method from OpenStax Prealgebra 2e, section 5.2: https://openstax.org/books/prealgebra-2e/pages/5-2-decimal-operations"
    },
    {
      "given": {
        "a": "0.1",
        "op": "+",
        "b": "0.2"
      },
      "expect": {
        "result": "0.3",
        "fraction": "3/10"
      },
      "source": "hand calculation in content.mdx: 1 + 2 = 3 tenths; Python 3: Decimal(\"0.1\") + Decimal(\"0.2\") = 0.3"
    },
    {
      "given": {
        "a": "2.5",
        "op": "*",
        "b": "0.04"
      },
      "expect": {
        "result": "0.1",
        "steps": "Multiply without the decimal points: 25 × 4 = 100; 1 + 2 = 3 decimal places, so the product is 0.100 = 0.1"
      },
      "source": "hand calculation in content.mdx; Python 3: Decimal(\"2.5\") * Decimal(\"0.04\") = 0.100"
    },
    {
      "given": {
        "a": "7.5",
        "op": "/",
        "b": "0.25"
      },
      "expect": {
        "result": "30",
        "steps": "Move both decimal points 2 places to the right: 750 ÷ 25; Divide: 750 ÷ 25 = 30"
      },
      "source": "hand calculation in content.mdx; Python 3: Decimal(\"7.5\") / Decimal(\"0.25\") = 30"
    },
    {
      "given": {
        "a": "1",
        "op": "/",
        "b": "0.3",
        "dp": 4
      },
      "expect": {
        "result": "3.3̅",
        "rounded": "3.3333",
        "fraction": "10/3"
      },
      "source": "hand calculation in content.mdx: 10 ÷ 3 = 3.333…; Python 3: Fraction(1) / Fraction(\"0.3\") = 10/3"
    },
    {
      "given": {
        "a": "5.2",
        "op": "-",
        "b": "8.75"
      },
      "expect": {
        "result": "-3.55",
        "steps": "Line up the decimal points with 2 decimal places each: 5.20 and 8.75; Subtract as whole numbers: 520 − 875 = -355; Put the decimal point back 2 places from the right: -3.55"
      },
      "source": "hand calculation in content.mdx; Python 3: Decimal(\"5.2\") - Decimal(\"8.75\") = -3.55"
    },
    {
      "given": {
        "a": "-1.5",
        "op": "*",
        "b": "-0.3"
      },
      "expect": {
        "result": "0.45",
        "problem": "-1.5 × (-0.3)"
      },
      "source": "hand calculation in content.mdx: 15 × 3 = 45, 2 decimal places, and negative times negative is positive"
    }
  ],
  "sources": [
    "OpenStax, Prealgebra 2e, section 5.2 Decimal Operations (add, subtract, multiply, and divide decimals): https://openstax.org/books/prealgebra-2e/pages/5-2-decimal-operations",
    "OpenStax, Prealgebra 2e, section 5.3 Decimals and Fractions (repeating decimals): https://openstax.org/books/prealgebra-2e/pages/5-3-decimals-and-fractions"
  ],
  "related": [
    "fraction-to-decimal",
    "multiplication",
    "rounding",
    "division",
    "fraction"
  ],
  "changelog": []
}
