Converting Between Numbers
Casts and Convert methods.
Converting Between Numbers is a free C# Academy lesson on CoddyKit — lesson 4 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 Convert?
Different numeric types can't always mix freely. Sometimes you must convert a value from one type to another.
C# offers implicit conversions, explicit casts, the Convert class, and parsing from text. This lesson covers each.
Implicit Conversion
When the target type can hold every possible value of the source, C# converts implicitly with no extra syntax.
An int fits inside a double, so this assignment is automatic and safe.
int whole = 7;
double real = whole;
Console.WriteLine(real);Explicit Casting
Going the other way can lose data, so C# requires an explicit cast with the target type in parentheses.
Casting a double to int truncates the fraction toward zero.
double price = 9.99;
int dollars = (int)price;
Console.WriteLine(dollars);Casting Can Overflow
Casting a value that doesn't fit the target type can produce a wrong result through overflow.
Casting a large long into an int wraps around, so always confirm the value fits.
long big = 3000000000;
int small = (int)big;
Console.WriteLine(small);The Convert Class
The System.Convert class converts between types with methods like Convert.ToInt32 and Convert.ToDouble.
Unlike casting, Convert.ToInt32 rounds a double to the nearest integer rather than truncating.
double value = 9.99;
int rounded = Convert.ToInt32(value);
Console.WriteLine(rounded);Parsing Numbers from Text
User input and files arrive as string. Use int.Parse or double.Parse to turn text into numbers.
The text must be a valid number, or parsing throws an exception.
string text = "42";
int number = int.Parse(text);
Console.WriteLine(number + 8);Safe Parsing with TryParse
int.TryParse attempts a conversion and returns true or false instead of throwing. The parsed value comes back through an out parameter.
This is the safe way to handle untrusted input.
bool ok = int.TryParse("abc", out int result);
Console.WriteLine(ok);
Console.WriteLine(result);Numbers to Strings
Every numeric type has a ToString method to produce text. You can pass a format string to control the look.
"C"currency"F2"fixed with 2 decimals"N0"grouped thousands
double amount = 1234.5;
Console.WriteLine(amount.ToString("F2"));
Console.WriteLine(amount.ToString("N0"));int to double for Division
A frequent reason to convert is to avoid integer division. Cast one operand to double before dividing to keep the decimals.
Without the cast you would get a truncated whole-number result.
int scored = 7, total = 10;
double percent = (double)scored / total * 100;
Console.WriteLine(percent);Casting vs Convert vs Parse
Choose the right tool:
- Cast between numeric types (truncates)
- Convert between many types (rounds, handles null)
- Parse / TryParse turns text into numbers
Prefer TryParse for any input you don't control.
A Conversion Example
This complete program reads a number from text, converts it, computes with it, and formats the output as currency.
string input = "19.95";
if (decimal.TryParse(input, out decimal price))
{
decimal withTax = price * 1.20m;
Console.WriteLine(withTax.ToString("F2"));
}Quick Check
Pick the safest way to convert user-typed text into a number.
Recap
You learned to convert numbers: implicit conversions when safe, explicit casts (which truncate and can overflow), the Convert class (which rounds), and parsing text with Parse and the safer TryParse.
You also formatted numbers back to strings with ToString format codes. Cast one operand to double to avoid integer division.
Frequently asked questions
Is the “Converting Between Numbers” lesson free?
Yes — the full text of “Converting Between Numbers” 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 “Converting Between Numbers”?
Casts and Convert methods. 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 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Converting Between Numbers” 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
- Numeric Types
- Arithmetic and Precedence
- The Math Class
- Converting Between Numbers