int, float, double, decimal
In this page:
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
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: