C# decimal (High Precision)
The decimal data type is used for financial and monetary calculations that require high precision. It offers greater precision than both float and double types.
Key Concepts
- The
decimaltype is a 128-bit high-precision floating-point number. - It is ideal for financial, banking, or currency-based calculations.
- Always append an 'M' to the value to explicitly mark it as a
decimal.
Example of Using a decimal
Code Example
// Declare a decimal variable
decimal myDecimal = 19.99M;
// Output the decimal to the console
Console.WriteLine(myDecimal);
Output:
19.99
Code Explanation: The decimal variable myDecimal is initialized with the value 19.99. The 'M' denotes that the number is a decimal. It is printed using the Console.WriteLine() method.
Output Explanation: The value 19.99 is displayed on the console, which is the value assigned to myDecimal.