DigiCalc

How Is Capital Gains Tax Calculated on Stocks?

· 8 min read

Whether you just sold stocks, crypto, or a rental property, understanding how capital gains tax is calculated is the key to knowing what you'll actually owe — and what you can do to reduce it. The math isn't complicated, but knowing which rate applies (and when) can save you thousands of dollars on the same exact gain.

What counts as a capital gain?

A capital gain is the profit you make when you sell a capital asset — stocks, bonds, mutual funds, ETFs, cryptocurrency, real estate, or collectibles — for more than you paid. If you sell for less than you paid, you have a capital loss, which can actually reduce your overall tax bill.

Capital gains are realized when you sell. Paper gains on positions you still hold don't trigger taxes — only the sale does. This is why timing a sale to a different tax year, or even waiting a few extra days past the one-year holding mark, can make a meaningful difference.

Short-term vs long-term: the rate difference that changes everything

The single most important factor in how capital gains tax is calculated is your holding period — how long you owned the asset before selling.

  • Short-term capital gains — assets held one year or less. Taxed at your ordinary income rate, the same as wages. Federal rates range from 10% to 37% depending on your total income.
  • Long-term capital gains — assets held more than one year. Taxed at preferential rates of 0%, 15%, or 20%. Most households pay 15%.

The difference is dramatic. Say you have a $50,000 gain and you're in the 32% ordinary income bracket. As a short-term gain, you owe $16,000 in federal tax. Hold just one more day past the one-year mark and the same gain at 15% costs $7,500 — a difference of $8,500 from patience alone.

How capital gains tax is calculated: a step-by-step example

Step 1: Find your cost basis

Your cost basis is what you paid for the asset, including brokerage commissions. For stocks, it's typically the purchase price multiplied by the number of shares, plus any fees paid. If you bought the same stock at different times and prices, you need to choose a cost basis method:

  • FIFO (First In, First Out) — the brokerage default. Assumes you sell your oldest shares first.
  • Specific Identification — you choose exactly which shares to sell. Useful when your oldest shares have a lower basis (more gain) and you'd prefer to sell higher-basis shares (less gain) instead.
  • Average Cost — permitted for mutual funds; divides total cost by total shares.

Choosing the right cost basis method before you sell can meaningfully reduce your tax obligation, especially for positions built up over many years at varying prices.

Step 2: Calculate the gain

Capital gain = Sale proceeds − Cost basis − Selling costs

Example: You bought 100 shares at $45/share ($4,500 total). You sell them for $72/share ($7,200 total). Your capital gain is $7,200 − $4,500 = $2,700.

Step 3: Determine the holding period

The IRS counts your holding period from the day after your purchase date to the date of sale. If you bought on January 10 last year and sell on January 10 this year, that's exactly one year — still short-term. You need to sell on January 11 or later for it to qualify as long-term.

Step 4: Apply the correct rate

For long-term gains, your rate depends on your total taxable income for the year. For 2025 (filed in 2026):

  • Single filers: 0% up to $48,350 of taxable income; 15% from $48,351 to $533,400; 20% above that.
  • Married filing jointly: 0% up to $96,700; 15% from $96,701 to $600,050; 20% above.

These thresholds apply to your total taxable income including the gain. If part of a long-term gain pushes you across a bracket boundary, only the portion above the threshold gets taxed at the higher rate — it works just like ordinary income brackets.

Worked example: You're a single filer with $40,000 in ordinary taxable income (after the standard deduction) and a $20,000 long-term capital gain. The first $8,350 of the gain fills the 0% bracket up to the $48,350 limit. The remaining $11,650 is taxed at 15%. Total federal capital gains tax: $1,748. As a short-term gain at the 22% bracket, the same $20,000 would cost $4,400 — more than double.

The net investment income tax (NIIT)

High earners face an additional 3.8% surtax on top of regular capital gains rates. The Net Investment Income Tax (NIIT) applies to the lesser of your net investment income or the amount by which your modified adjusted gross income (MAGI) exceeds:

  • $200,000 for single filers
  • $250,000 for married filing jointly

These thresholds are not adjusted for inflation, so they catch more earners each year. A single filer earning $215,000 with $20,000 in long-term gains could owe 15% + 3.8% = 18.8% on some of those gains. The capital gains tax calculator factors in the NIIT automatically alongside the standard rates, so you get the complete federal picture in one step.

How capital losses reduce your tax bill

Tax-loss harvesting is the practice of selling investments at a loss to offset gains elsewhere in your portfolio. It's one of the most effective year-end tax moves available to individual investors.

  • Capital losses first offset gains of the same type (short-term losses against short-term gains; long-term losses against long-term gains). After same-type netting, excess losses of one type can offset the other type.
  • If total losses still exceed total gains, up to $3,000 of net capital loss can be deducted against ordinary income in a single year.
  • Any remaining net loss carries forward indefinitely to future tax years — nothing is wasted.

The wash-sale rule: If you sell a security at a loss and buy a substantially identical security within 30 days before or after the sale, the IRS disallows the loss. This applies to stocks, bonds, and ETFs tracking the same index. Note that cryptocurrency is currently not subject to the wash-sale rule, giving crypto holders more flexibility.

Special cases: crypto, inherited assets, and mutual funds

Cryptocurrency

The IRS treats cryptocurrency as property, not currency. Every sale, trade, or use of crypto to buy something is a taxable event. Crypto-to-crypto swaps (trading Bitcoin for Ethereum, for example) are also taxable — you realize a gain or loss based on the fair market value of what you received at the time of the swap. The same short-term and long-term holding periods and rates apply as for stocks.

Inherited assets

When you inherit an investment, you receive a stepped-up cost basis equal to the fair market value on the date of the original owner's death. This means you can sell inherited assets immediately with little or no capital gains tax, regardless of how much they appreciated during the deceased's lifetime. It's one of the most favorable treatments in the tax code.

Mutual funds and ETFs

Even if you don't sell fund shares, a mutual fund may distribute capital gains to all shareholders at year-end. These are taxable even if you reinvest them. ETFs are structured to minimize these distributions through an in-kind redemption process, making them more tax-efficient than actively managed mutual funds — one reason many tax-conscious investors prefer them for taxable accounts.

What to do before you sell

  1. Check your holding period precisely. Even a few days past the one-year mark shifts you from ordinary income rates to long-term rates. Don't sell early if you can hold out.
  2. Estimate your taxable income for the year. If you'll be in a lower-income year — job change, retirement, extended leave — that's the ideal time to realize large gains at a lower or even 0% long-term rate. The income tax estimator can show you your current bracket before you decide.
  3. Look for losses to harvest. Selling losing positions before year-end can offset your winners, reducing the net taxable gain.
  4. Consider which account to sell from. Gains inside a Roth IRA, traditional IRA, or 401k aren't taxed when realized — only on withdrawal (and never for Roth). Keeping high-growth assets in tax-advantaged accounts is a long-term strategy worth setting up now.
  5. Run the numbers before selling, not after. Once the sale is done, your options narrow considerably. A precise estimate beforehand turns a surprise tax bill into a planned expense.

See your capital gains tax estimate in seconds

Try the Capital Gains Tax Calculator →

Capital gains tax is one of the most controllable taxes in the code — holding period, loss harvesting, account selection, and income timing all give you real levers. The key is knowing your numbers before you sell. Whether you're dealing with stocks, crypto, or a rental property, running the calculation in advance turns a vague anxiety into a concrete plan.

If you're also thinking about how capital gains fit into your long-term wealth picture, see our guide on whether you're on track for retirement — asset location inside tax-advantaged accounts is one of the biggest structural advantages available to long-term investors.