#49 – When to Use the Decimal Type

You should use the decimal type (System.Decimal) for monetary calculations, or whenever you want more digits of precision, to avoid round-off error.

For example:

 float f1 = 12345678901234567.28f;     // 1.23456784E+16
 double d1 = 12345678901234567.28d;    // 12345678901234568.0
 decimal dc1 = 12345678901234567.28m;  // 12345678901234567.28

The decimal type gives you greater precision, but doesn’t support storing numbers as large as float or double.  This is because it stores all digits, rather than storing the mantissa and exponent of the number.  The decimal type also requires more storage space.

float – 4 bytes  (±1.5e−45 to ±3.4e38, 7 digit precision)
double – 8 bytes  (±5.0e−324 to ±1.7e308, 15-16 digit precision)
decimal – 16 bytes  (±1.0 × 10−28 to ±7.9 × 1028, 28-29 digit precision)


About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: