If you’ve been thinking about replacing your old car with a newer model, you’ve probably considered getting a hybrid. It gets better gas mileage, is earth-friendly, and you’ll probably impress all of your friends with your commitment to the environment.

Let’s disregard the feel-good factors and look purely at the economics of getting a hybrid car simply because it’s a hybrid. How can you determine if you’re getting a good deal? If you have to pay more for a hybrid, then how long do you have to keep it to have that higher MPG work in your favor?

As it turns out, doing the calculation is not that difficult. It’s a similar approach to the calculation you would use to determine if refinancing your mortgage is viable.

First, you need to establish some basic information. I’m going to be making up numbers for this example because every case is different. So, let’s pretend that you’re looking at buying a hybrid that costs $20,000 and gets 50 mpg versus a non-hybrid that costs $15,000 and gets 35 mpg. Straightaway, you can see that the non-hybrid is $5,000 cheaper than the hybrid, but it also gets 15 mpg less than the hybrid.

Next, look at the number of miles that you drive annually and the cost of gas in your area. Let’s assume that you drive 15,000 miles per year and that gas costs $4 per gallon. If you drive the hybrid, that means that you’ll need 300 gallons per year (15,000/50), which means that gasoline will cost you $1,200 per year. If you drive the non-hybrid, you’ll need 428.6 gallons per year (15,000/35), which will cost you $1,714.29 per year in gas. That’s a $514.29/year difference.

If maintenance costs are going to be different, then you’ll need to add the difference into your calculations. If maintaining the hybrid costs $250/year more, then that would mean a $264.29/year difference, as the additional maintenance cost will eat into the gasoline savings.

Once you have the total cost differential, you take the purchase price difference and divide it by the cost difference to get a breakeven point – the number of years you’ll have to own a car in order for the difference in purchase price to be made up in the operating savings. In this case, it would be 18.9 years.

But, wait, you say. You could take the money you saved at purchase time and invest it. This is true, and if you do save or invest the difference, it will grow over time, throwing off cash which you could, theoretically, use to offset the cost difference. If you could get a 5.29% return, then you could use the excess cash to pay for the difference. This is simply the operating cost difference / payment difference.

However, getting a tax credit does affect the difference in purchase price. Let’s say that you get a $1,000 federal tax credit for buying a hybrid, and your effective federal tax rate is 20%. That means that you’d need $1,250 in after tax dollars – the tax credit / (1- effective federal tax rate) to get the same $1,000 in pre-tax dollars, since a tax credit reduces your tax liability dollar for dollar.

In this case, a $1,000 tax credit with a 20% effective federal tax rate would make the after-tax difference in price $3,750. You’d need to invest at a 7.05% return to be able to make up the difference in operating costs, and if you didn’t invest the return, you’d have a breakeven at 14.19 years.

If you can’t invest at that rate, but can invest at a lower rate, then you can use the following equation to calculate for the breakeven number of years:

Breakeven years = – [ln(1-[difference in purchase price/difference in operating cost])]/ln(1+rate of return)

It’s probably easier to use Excel’s PV and Solver.

So, next time you’re looking at a car and want to figure out if the gasoline savings are worth the extra purchase price, you can use these frameworks to help you figure out how long you’ll have to keep that hybrid to make it worth your while. According to Consumer Reports, the average new car lasts between 8 and 15 years and 150,000 and 300,000 miles.

You may also find Edmunds’s total cost of ownership (TCO) calculator helpful in comparing cars.

If you want to read about our experience with a hybrid, you can read about our issue with a Prius battery and what we decided to do about it.