Saturday, December 16, 2023

Floating Point Numeric Data types

.Net supports three floating point numeric data type.

Below are all the floating point numeric data types supported by .net and their corresponding c# data types

You can choose one of these types for you variables and constants depending upon your requirement (range of the data and its precision).

Decimal (decimal) is high precision but with less range.

Single (float) and Double(double) are low precision but with high range.

Calculation performance of Decimal is much slower that Float and Double.

.Net Type C# Type Size in bytes Range Precision Suffix
System.Single float 4 ±1.5 x 10-45 to ±3.4 x 1038 ~6-9 digits F/f
System.Double double 8 ±5.0 x 10-324 to ±1.7 x 10308 ~15-17 digits D/d
System.Decimal decimal 16 ±1.0 x 10-28 to ±7.9228 x 1028 28-29 digits M/m

When using a const floating point number in the program by default it is considered as double.

If you want to change its type to float or decimal, you can use the suffix


var len = 245.5; // double
var size = 245.5F; // float
var age = 245.5M; // decimal

No comments:

Post a Comment

Object Oriented Programming- 2- Inheritance

Inheritance is one of the fundamental attributes of object-oriented programming. It allows you to define a child class...