int, float, double, decimal
int, float, double, and decimal are boxes for numbers of different kinds: whole numbers, and numbers with decimal points at different levels of precision. Choose decimal for money, like picking a fine ruler when exactness matters.
In this page:
Syntax
int wholeNumber = 30;
float singleValue = 36.6f;
double doubleValue = 12345.6789;
decimal money = 19.99m;
int, float, double, decimal
int stores whole numbers, float and double store approximate binary floating-point numbers, and decimal stores base-10 numbers precisely — ideal for money.
double is the default for floating-point literals; float needs an f suffix and decimal needs an m suffix. Choosing the right numeric type avoids subtle rounding bugs.
Note:
Use
decimal for currency calculations to avoid floating-point rounding errors.Example: int, float, double, decimal
using System;
class Program
{
static void Main(string[] args)
{
int age = 30;
float temperature = 36.6f;
double distance = 12345.6789;
decimal price = 19.99m;
Console.WriteLine(age);
Console.WriteLine(temperature);
Console.WriteLine(distance);
Console.WriteLine(price);
}
}
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
Common Mistakes
- Writing
float f = 3.14;, which fails because3.14is adouble; use3.14f. - Using
doublefor money and getting rounding errors, whendecimalis meant for currency. - Dividing two ints like
5 / 2and expecting 2.5, when integer division gives 2.
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: