Despite rising home prices and climbing mortgage rates, it's still cheaper to buy a home than rent one in major cities across the country, according to real estate web site Trulia, which analyzed data in 100 metro areas.

But home prices are just one factor to consider. Deciding whether to buy or rent also depends on the location and how long you plan to stay there. In most of the Rust-Belt cities, like Toledo and Detroit, the math overwhelmingly favors buying. In more expensive coastal markets, like Los Angeles and New York, it's a closer call.

Nationwide, homebuyers who remain in their homes for seven years will save an average of 38% over renting, Trulia found. A year ago, buying was 44% cheaper.

That means all of the initial transaction costs of buying a home -- the broker's commission, title insurance, legal fees and other closing costs -- will be offset by benefits, like tax write-offs and price appreciation. And those costs will become cheaper than the total costs of renting, which include insurance and agent commissions.

Source: Trulia In order to get a more accurate comparison, Trulia estimated sale prices and rents for similar homes in similar neighborhoods, instead of using the average sale price or rental rate. It further calculated the initial total monthly costs of owning and renting, including maintenance, insurance and taxes, as well as future costs of owning and renting, such as price and rent appreciation and inflation. Trulia also included one-time transactional costs, including down payments, sales proceeds and security deposits. It assumes that buyersget a 30-year fixed mortgage with a 4.5% rate and put 20% down and that residents stay in their home for at least seven years.