How to Mix int and decimal Values in One Calculation in C#
hours * hourlyRate, an int times a decimal, needs no conversion code: C# turns the int into a decimal first, and the result is a decimal. Going back from decimal to int is the step that needs a cast.
The C# reference: C# provides a set of integral and floating-point numeric types. An implicit or explicit conversion exists between any two numeric types. Use a cast expression to perform an explicit conversion. An implicit conversion happens by itself, and an explicit one is a cast you write, such as (int).
In a calculation with two different types, the reference says: If the operands are different integral or floating-point types, their values are converted to the closest containing type, if such a type exists.
Syntax
<whole> <operator> <decimal or double>
(<type>)<value>
| Part | What it is |
|---|---|
<whole> | A whole number of type int, such as hours or 500 |
<operator> | An arithmetic or comparison operator, such as *, / or >= |
<decimal or double> | A number of type decimal or double. The int is converted to its type before the operator runs |
(<type>)<value> | A cast, such as (int)pay: the explicit conversion you need to go from decimal or double to int |
The rules, in the reference's words: Any integral numeric type is implicitly convertible to any floating-point numeric type. And for the result: You can also mix integral types and the decimal type in an expression. In this case, integral types are implicitly converted to the decimal type and the expression evaluates to decimal, or to bool in relational and equality comparisons.
The Example
A console app called IntDecimal works out a week's pay from 38 hours at 15.75 an hour, turns 90 minutes into hours, compares the pay with 500, and casts the pay back to an int.
Prerequisites
- The .NET 10 SDK.
dotnet --versionprints a version that starts with10.0.
Step 1: Create a Console App
dotnet new console -n IntDecimal
cd IntDecimal
The template "Console App" was created successfully.
Processing post-creation actions...
Restoring /Users/you/source/IntDecimal/IntDecimal.csproj:
Determining projects to restore...
Restored /Users/you/source/IntDecimal/IntDecimal.csproj (in 46 ms).
Restore succeeded.
dotnet new console -n IntDecimal creates a folder named IntDecimal with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or IntDecimal.csproj in Visual Studio.
Step 2: Write the Code
Replace everything in Program.cs. First the culture and the pay:
using System.Globalization;
// Print numbers the same way on every computer.
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
// An int times a decimal: the int becomes a decimal first.
int hours = 38;
decimal hourlyRate = 15.75m;
var pay = hours * hourlyRate;
Console.WriteLine($"{hours} * {hourlyRate} = {pay} ({pay.GetType().Name})");
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;makes the program print a full stop before decimals on every computer.var pay = hours * hourlyRate;lets C# choose the type ofpayfrom the calculation, andpay.GetType().Nameprints which type that is.
Then a double and a comparison:
// An int divided by a double: the int becomes a double.
int minutes = 90;
var inHours = minutes / 60.0;
Console.WriteLine($"{minutes} / 60.0 = {inHours} ({inHours.GetType().Name})");
// An int in a comparison with a decimal works the same way.
Console.WriteLine($"{pay} >= 500 is {pay >= 500}");
minutes / 60.0divides anintby thedoubleliteral60.0.pay >= 500compares thedecimalpay with theint500.
Then the cast:
// Back to an int takes a cast, which drops the fraction.
int wholePay = (int)pay;
Console.WriteLine($"(int){pay} = {wholePay}");
(int)pay is an explicit conversion: you have to ask for it, because the fraction can be lost.
Step 3: Run It
Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:
dotnet run
38 * 15.75 = 598.50 (Decimal)
90 / 60.0 = 1.5 (Double)
598.50 >= 500 is True
(int)598.50 = 598

38 * 15.75gave 598.50, aDecimal: theint38 became adecimalbefore the multiplication.DecimalandDoubleare the .NET names thatGetType().Nameprints fordecimalanddouble.90 / 60.0gave 1.5, aDouble.598.50 >= 500wasTrue: theint500 became adecimalfor the comparison.(int)598.50gave 598: the cast dropped the fraction. The reference: When you convert a decimal value to an integral type, this value is rounded toward zero to the nearest integral value.
The Complete File

using System.Globalization;
// Print numbers the same way on every computer.
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
// An int times a decimal: the int becomes a decimal first.
int hours = 38;
decimal hourlyRate = 15.75m;
var pay = hours * hourlyRate;
Console.WriteLine($"{hours} * {hourlyRate} = {pay} ({pay.GetType().Name})");
// An int divided by a double: the int becomes a double.
int minutes = 90;
var inHours = minutes / 60.0;
Console.WriteLine($"{minutes} / 60.0 = {inHours} ({inHours.GetType().Name})");
// An int in a comparison with a decimal works the same way.
Console.WriteLine($"{pay} >= 500 is {pay >= 500}");
// Back to an int takes a cast, which drops the fraction.
int wholePay = (int)pay;
Console.WriteLine($"(int){pay} = {wholePay}");
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Putting the result in an int, as in int pay = hours * hourlyRate; | error CS0266: Cannot implicitly convert type 'decimal' to 'int'. An explicit conversion exists (are you missing a cast?) The reference: There are no implicit conversions from the double and decimal types. | Store it in a decimal, or cast on purpose with (int) |
Mixing a decimal and a double, as in hourlyRate * 1.5 | error CS0019: Operator '*' cannot be applied to operands of type 'decimal' and 'double' The reference: There are no implicit conversions between the decimal type and the float or double types. | Write the literal as a decimal: hourlyRate * 1.5m gave 23.625 |
Dividing two int values into a decimal, as in decimal perDay = hours / 4; | It gave 9, not 9.5: 38 / 4 ran as int division, and only its result became a decimal | Make one side a decimal: hours / 4m gave 9.5 |
FAQ
Which conversions happen by themselves?
The ones in the reference's table of implicit conversions. For int, it lists long, float, double, decimal, or nint. An int went into a long, a double and a decimal without a cast, and all three held 38. The other way, int whole = inHours; with a double gave error CS0266: Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists (are you missing a cast?)
Conclusion
In a calculation, an int adapts to a decimal or a double: it's converted to that type by itself, and the result has that type. Going back to int takes a cast, which drops the fraction, and a decimal and a double never mix without one.
Additional Resources
- Built-in numeric conversions — which conversions are implicit and which need a cast.
- Floating-point numeric types — mixing whole numbers with decimal and double.
- Arithmetic operators — how operands of different types are converted.
