Decimal Data Type in C# and How to Use It?

Decimal Data Type in C# and How to Use It?

Decimal Data Type in C#. After talking about value types and reference types in our last article, let’s talk about decimal data types in C#. Decimal data types are very important in a programming language. These are the types that show precision in any number. Like the economy of a country or the percentage of population growth in a country, it could be anything.

We have got 3 types of decimal types in C#. They have different sizes and libraries. Please take a look at the table below.

floatSystem.Single4 bytes7 digits
doubleSystem.Double8 bytes15 digits excluding decimal point
decimalSystem.Decimal16 bytes29 decimal places

The decimal places that you see in the table above. like 7 digits in a float. That 7 includes the places before and after the decimal point (.) and excludes the decimal from 7.

Float Example

float a = 23.123456f;

The float can take up to 7 digit numbers but in reality, we get a lot of bigger numbers in comparison to 23.123456f. That is why we have double. The size of double is 8 bytes and can take up to 15 digits excluding decimal point (.).

Double Example

double a = 23.12345678912345d

Just like double we have another decimal data type, that type is called decimal literally. Decimal type can take up to 29 digits number including before decimal point and after the decimal point.

Decimal Example

decimal a = 23.123456789123456789000000000m

While researching for this toping, I found different precision numbers but doing practical I found different. So I have mentioned here that I found during my testing. The example numbers I ran in the visual studio and got different results from Microsoft site. I hope the information is helpful. In case you have any confusion, feel free to comment. I would be glad to help you understand them.

Till then take care guy. Stay safe from Covid 19. Have a good one!

Read more