0Pricing
C# Academy · Lesson

Numeric Types

int, double, decimal and more.

Numeric Types is a free C# Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Numeric Types Matter

C# offers several numeric types, each storing a different range of values and using a different amount of memory.

Choosing the right one keeps your programs correct and efficient. A whole number like the count of users fits an int; a price needs decimals.

In this lesson you'll meet integers, floating-point, and decimal types.

Integer Types

Integers store whole numbers with no fractional part.

  • int — 32-bit, the everyday default
  • long — 64-bit, for very large counts
  • short and byte — smaller ranges

Use int unless a value can exceed about two billion.

int age = 30;
long population = 8000000000;
Console.WriteLine(age);
Console.WriteLine(population);

Integer Ranges

Each integer type has fixed minimum and maximum values. You can read them with int.MaxValue and similar constants.

Going past the maximum causes overflow, where the value wraps around silently.

Console.WriteLine(int.MinValue);
Console.WriteLine(int.MaxValue);
Console.WriteLine(long.MaxValue);

Floating-Point: double

double is a 64-bit floating-point type for real numbers with decimals.

It is the default for fractional literals, so 3.14 is a double. It is fast and great for science and graphics, but stores values approximately.

double pi = 3.14159;
double half = 1.0 / 2.0;
Console.WriteLine(pi);
Console.WriteLine(half);

Floating-Point: float

float is a 32-bit floating-point type. It uses half the memory of double but holds fewer digits.

A float literal needs an f suffix, like 2.5f, otherwise C# treats the number as a double.

float temperature = 21.5f;
float ratio = 0.1f;
Console.WriteLine(temperature);
Console.WriteLine(ratio);

Precise Money: decimal

decimal is a 128-bit type designed for money and exact decimals. It avoids the tiny rounding errors of double.

A decimal literal uses an m suffix, like 9.99m. Use it for prices, banking, and accounting.

decimal price = 9.99m;
decimal tax = 0.08m;
Console.WriteLine(price + price * tax);

double vs decimal

Watch how double can produce a surprising result while decimal stays exact.

This is why financial code prefers decimal even though it is slower than double.

double d = 0.1 + 0.2;
decimal m = 0.1m + 0.2m;
Console.WriteLine(d);
Console.WriteLine(m);

Type Inference with var

The keyword var lets the compiler infer the type from the value on the right.

The variable still has a fixed type — var count = 5; makes an int. Suffixes decide the type for fractional literals.

var count = 5;
var pi = 3.14;
var price = 9.99m;
Console.WriteLine(count.GetType());
Console.WriteLine(price.GetType());

Digit Separators

For readability, you can place underscores between digits in any numeric literal. The underscores are ignored by the compiler.

This makes large numbers far easier to read at a glance.

int million = 1_000_000;
long distance = 9_460_730_472_580_800;
Console.WriteLine(million);
Console.WriteLine(distance);

Special double Values

double can represent special values: positive and negative infinity, and NaN (not a number).

Dividing by zero with doubles yields infinity instead of an error, while integer division by zero throws an exception.

double inf = 1.0 / 0.0;
double nan = 0.0 / 0.0;
Console.WriteLine(inf);
Console.WriteLine(nan);

Choosing the Right Type

A quick guide:

  • Whole numbers → int (or long if huge)
  • Scientific or general decimals → double
  • Money and exact values → decimal

Picking well prevents overflow bugs and rounding errors.

Quick Check

Test your understanding of numeric types.

Recap

You learned C#'s core numeric types: int and long for whole numbers, double and float for floating-point, and decimal for exact money values.

Remember the literal suffixes: f for float, m for decimal, and underscores for readability. Choose types to balance range, precision, and memory.

Frequently asked questions

Is the “Numeric Types” lesson free?

Yes — the full text of “Numeric Types” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “Numeric Types”?

int, double, decimal and more. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Numeric Types” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Numeric Types
  2. Arithmetic and Precedence
  3. The Math Class
  4. Converting Between Numbers
← Back to C# Academy