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.
|float||System.Single||4 bytes||7 digits|
|double||System.Double||8 bytes||15 digits excluding decimal point|
|decimal||System.Decimal||16 bytes||29 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 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 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 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!