HiveBrain v1.2.0
Get Started
← Back to all entries
principlecsharpCritical

decimal vs double! - Which one should I use and when?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
doubleshouldandusedecimalwhenonewhich

Problem

I keep seeing people using doubles in C#. I know I read somewhere that doubles sometimes lose precision.
My question is when should a use a double and when should I use a decimal type?
Which type is suitable for money computations? (ie. greater than $100 million)

Solution

For money, always decimal. It's why it was created.

If numbers must add up correctly or balance, use decimal. This includes any financial storage or calculations, scores, or other numbers that people might do by hand.

If the exact value of numbers is not important, use double for speed. This includes graphics, physics or other physical sciences computations where there is already a "number of significant digits".

Context

Stack Overflow Q#1165761, score: 1272

Revisions (0)

No revisions yet.