Humor me while I tell a little story.

You're walking to work one day when you spot a dollar bill lying on the sidewalk. You pick it up and, feeling a little superstitious, spend it on a lottery ticket. The next day, you find you've won $5,000. Great news! You take the winnings and buy a long-shot biotech stock -- Dendreon, for instance. Let's say this day was in late March, and you were able to buy 1,000 shares of Dendreon at $5 each.

The stock pops the next day on rumors of an imminent FDA approval, and you sell out a few days later at $22. $22,000! You're really feeling your luck now, and you decide to push it -- by taking that money to a casino. You hit the roulette wheel and put it all on number 22, a 35-to-1 bet, and win $770,000. You let it ride -- keep all the money on the same number -- and win again. Now you're up to a whopping $26,950,000. You feel that nothing can stop you now and let it ride again -- but the little ball drops in 23 instead.

Oops. You're wiped out.

Now here's the question: How much did you really lose? A dollar? $22,000? Almost $27 million? Nothing?

We could make an argument for any of those answers, but the truth is, it depends on how you look at it. And how you look at it depends on your mental accounting. 

Your internal filing system
Mental accounting is a cornerstone of behavioral finance -- the study of why people sometimes do things with their money that don't seem rational. First propounded by Richard Thaler of the University of Chicago, the idea behind mental accounting is that people tend to put more value on some dollars than others. Take the example above: Suppose that instead of winning it at a casino, you had inherited that $26,950,000 from your beloved grandmother, who had shepherded the family fortune through decades of ups and downs.

Most of us wouldn't dream of betting that kind of legacy on one spin of a roulette wheel. You'd want to buy stocks that Grandma had heard of and had confidence in -- something like General Electric (NYSE:GE), which just reported another business-as-usual solid quarter. If you were willing to take a bit more risk, you might invest in General Motors (NYSE:GM), which, despite its well-publicized troubles at home, is seeing surprisingly strong sales growth overseas.

But the closest Grandma's cash would get to the craps table would be in casino stocks like Las Vegas Sands (NYSE:LVS), which has seen explosive growth thanks to great new properties -- and which, coincidentally, generates piles of cash from poor mental accounting (specifically, that tendency of people to keep betting "play money" until it's all gone, like in the example above).

That's mental accounting -- dollars from Grandma are just different from dollars won in a crazy run at the roulette table, even though they represent the same amount of buying power and security for one's family. And to get back to the original question, how much you feel you "lost" depends on at what point -- if any -- the proceeds that followed from that found dollar started, or stopped, feeling like real money.

When mental accounting goes bad
Sometimes, the results of mental accounting are just silly, or at least mostly harmless -- designating a surprise $300 bonus as "mad money" and blowing it, for instance, even if you know you really should have put it in savings. But sometimes, it can really work to your disadvantage. If you treat a huge tax refund as "free money" to blow rather than as part of your hard-earned income, or if you're more likely to overspend when you're using a credit card than when you're spending cash, then you're engaging in mental accounting -- and you could be getting hurt by it.

Here's another example: Suppose you have $10,000 in a short-term emergency account paying 4.5% interest, and $9,000 in credit card debt on which you're paying 18%. If you were to pay off the credit card, that card could be your "emergency fund" while you rebuild the savings account -- and you'd save faster if you weren't paying that 18%. But if your mental accounting has made that short-term account untouchable, that rational choice can be awfully hard to make -- or even see.

So what to do? Like anything else, the first step toward making better decisions is being aware of the mental habits that lead to less-good decisions. When you spot yourself engaging in mental accounting, remind yourself that a dollar is a dollar is a dollar -- and take a deep breath before proceeding. You may decide to proceed anyway, but you and your finances will feel better about it in the long run if your decision was a considered choice.

Want more ideas on spending and saving Foolishly? Check out the Motley Fool Green Light newsletter service. Each month's issue offers $450 worth of solid money-saving tips -- guaranteed. Get full access free for 30 days -- there's no obligation to buy.

If you'd like to learn more about mental accounting and other bits of behavioral finance, Fool contributor John Rosevear heartily recommends Why Smart People Make Big Money Mistakes -- and How to Correct Them by Gary Belsky and Thomas Gilovich, from which the example at the beginning of this article was loosely adapted. John does not own shares in any of the companies mentioned. The Motley Fool has a disclosure policy.