There are some simple and elegant question I received from an interviewer today and I would like discuss the same with you now.
- What is the difference between double and decimal?
- What is your choice in case if you want to hand money related transaction in your application?
- Which data type would you suggest or go with if you want to design a system related to financial transaction?
- What is your choice if you design the same in database – what data type would you prefer?
You will get questions similar to this in a typical DOT NET interview, answering each question right is not possible always. I said some bad answers there, so I dig down little bit and come up with my understanding in this article . I gave a bad answer in the interview since I did not have a considerable amount of experience in using double and decimal often , so look at the below table which contain the difference between double and decimal. Surely this will give you some insights about the difference between Double and Decimal.
|Size||64 bit||128 bit|
|Approximate range||±5.0 × 10−324 to ±1.7 × 10308||(-7.9 x 1028 to 7.9 x 1028) / (100 to 28)|
|Precision||15-16 digits||28-29 significant digits|
You have to consider three important points Size, Approximation range and Precision.
Decimal (128 bits) is big in size, It is almost twice the size of double(64) in storing the number of bit, and it has high precision supports up to 28-29 digits when compared to 15-16 digits in Double.
Range – Decimal (128 bit) holds smaller value range and higher precision when compared with Double, which makes it appropriate for financial and monetary calculations.
Double: The double keyword signifies a simple type that stores 64-bit floating-point values, has precision ranges between 15-16 digits only.
So Decimals are much powerful in terms of currency related transaction since it holds more precision. If understand this basic statics about these datatypes you can surely answer better.