Double vs Decimal
2023-03-07 C# float double decimal Floating-point types Data TypesI was recently asked about floating point data types, so I did a little analysis on differences between them. Here is a little table to summarize the properties of each popular type. Obviously largest benefit of decimal
type is that it is 10-based, so it does not loose precision in numbers like 0.1, which are periodic in 2-based double
.
float | double | decimal | |
---|---|---|---|
Size | 4 bytes | 8 bytes | 16 bytes |
Range | ±1.5 x 10−45 .. ±3.4 x 1038 | ±5.0 × 10−324 .. ±1.7 × 10308 | ±1.0 x 10-28 .. ±7.9228 x 1028 |
Precision | ~6-9 digits | ~15-17 digits | 28-29 digits |
Form | ± (1 + F/223) x 2E-127 F = 23 bit uint E = 8 bit uint |
± (1 + F/252) x 2E-1023 F = 52 bit uint E = 11 bit uint |
± V/10X V = 96 bit uint X = 0 .. 28 |
.NET Type | System.Single | System.Double | System.Decimal |