programming
decimal
data types
suffix notation
software development

Declaration suffix for decimal type

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In C#, the suffix for a decimal literal is m or M. That small suffix matters because numeric literals with a fractional part default to double, not decimal. If you want the precision and behavior of decimal, especially for money and exact base-10 values, you need to mark the literal explicitly.

Why the Suffix Exists

Different numeric types represent numbers differently. In C#:

  • 'int is for whole numbers'
  • 'double is the default floating-point type for decimal-looking literals'
  • 'decimal is a higher-precision base-10 type designed for financial and exact decimal work'

So this code creates a double:

csharp
var x = 12.5;
Console.WriteLine(x.GetType());

To create a decimal, add the suffix:

csharp
var y = 12.5m;
Console.WriteLine(y.GetType());

Without the suffix, the compiler treats the literal as the wrong numeric type for many decimal assignments and expressions.

Basic Decimal Literal Examples

Both lowercase and uppercase suffixes are valid.

csharp
1decimal a = 10.5m;
2decimal b = 10.5M;
3decimal c = 0.01m;
4decimal d = 100m;

All four literals are decimal values.

What Happens Without the Suffix

If you try to assign a decimal-looking literal directly to a decimal variable without the suffix, the compiler rejects it.

csharp
decimal price = 19.99m;   // correct
// decimal wrong = 19.99; // compile-time error

That happens because 19.99 is parsed as a double literal, and C# does not perform that conversion implicitly in this case.

Why decimal Is Used for Money

double is a binary floating-point type. Many decimal values cannot be represented exactly in binary floating-point, which can create tiny rounding artifacts.

csharp
1using System;
2
3double d = 0.1 + 0.2;
4decimal m = 0.1m + 0.2m;
5
6Console.WriteLine(d); // often 0.30000000000000004
7Console.WriteLine(m); // 0.3

This is why financial code often uses decimal. The suffix is not cosmetic. It is the point where you tell the compiler which arithmetic model you actually want.

Expressions Must Stay in the Decimal World

Even if one variable is decimal, mixed-type expressions can still cause problems if the literals are not suffixed.

csharp
1decimal subtotal = 100m;
2decimal taxRate = 0.075m;
3decimal total = subtotal + (subtotal * taxRate);
4
5Console.WriteLine(total);

If you accidentally write 0.075 without the suffix, the expression stops being consistent and the compiler may complain or force you into explicit casts.

Common Places the Suffix Is Needed

You usually need m in:

  • hard-coded prices
  • percentage rates
  • financial calculations
  • tests that compare exact decimal values

For example:

csharp
1decimal discount = 12.5m;
2decimal amount = 250m;
3decimal result = amount - (amount * discount / 100m);
4
5Console.WriteLine(result);

Every literal that should participate as a decimal is marked clearly.

Do Not Add the Suffix Blindly

Use decimal when exact decimal arithmetic is what the domain requires. Do not use it automatically for every numeric value. Scientific and graphics calculations often use double because range and performance characteristics differ.

The important point is to choose intentionally:

  • 'decimal for money and base-10 precision needs'
  • 'double for general floating-point work where binary floating-point is acceptable'

Once you choose decimal, the suffix keeps literals aligned with that choice.

Common Pitfalls

The most common mistake is forgetting the m suffix and then wondering why a decimal assignment fails to compile. Another is mixing decimal variables with unsuffixed double literals in expressions, which creates awkward conversion issues. Teams also sometimes choose decimal for every numeric problem even when they do not need its precision model. Finally, developers sometimes assume the suffix changes output formatting, when it actually changes the literal's type and arithmetic behavior.

Summary

  • In C#, the decimal literal suffix is m or M.
  • Decimal-looking literals default to double unless you add the suffix.
  • Use decimal for money and exact base-10 calculations.
  • Keep expressions consistent by suffixing decimal literals throughout the formula.
  • The suffix is about type correctness, not just syntax style.

Course illustration
Course illustration

All Rights Reserved.