Table of Contents

    Double Data Type in C# Programming Language: Definition and Usage

    Double Data Type in C# Programming Language: Definition and Usage

    Floating-Point Types in C#:

    Data Type Size (bytes) Precision Range Description
    float 4 7 decimal digits ±1.5 × 10^-45 to ±3.4 × 10^38 Single-precision floating-point number
    double 8 15-16 decimal digits ±5.0 × 10^-324 to ±1.7 × 10^308 Double-precision floating-point number
    decimal 16 28-29 significant digits ±1.0 × 10^-28 to ±7.9 × 10^28 Decimal floating-point number for financial calculations

    Examples Floating-Point Types in C#:

    Here’s a C# program that demonstrates the usage of floating-point types (float, double) and the decimal type:

    
    using System;
    
    class Program
    {
        static void Main()
        {
            // 32-bit floating point: ~ ±1.5 × 10⁻⁴⁵ to ±3.4 × 10³⁸ (7-digit precision)
            float floatValue = 3.1415927f;  // 'f' suffix indicates a float literal
            Console.WriteLine($"float: {floatValue}");
    
            // 64-bit floating point: ~ ±5.0 × 10⁻³²⁴ to ±1.7 × 10³⁰⁸ (15-16 digit precision)
            double doubleValue = 3.141592653589793;
            Console.WriteLine($"double: {doubleValue}");
    
            // 128-bit decimal: Used for financial and monetary calculations (28-29 digit precision)
            decimal decimalValue = 79228162514264337593543950335m;  // 'm' suffix indicates a decimal literal
            Console.WriteLine($"decimal: {decimalValue}");
            
            // Demonstrating precision difference between float, double, and decimal
            float smallFloatValue = 1.2345678f;  // 7 digits of precision
            double smallDoubleValue = 1.234567890123456;  // 15-16 digits of precision
            decimal smallDecimalValue = 1.2345678901234567890123456789m;  // 28-29 digits of precision
            
            Console.WriteLine("\nPrecision Example:");
            Console.WriteLine($"float (7-digit precision): {smallFloatValue}");
            Console.WriteLine($"double (15-16 digit precision): {smallDoubleValue}");
            Console.WriteLine($"decimal (28-29 digit precision): {smallDecimalValue}");
        }
    }
    
    

    Output:

    
    float: 3.141593
    double: 3.141592653589793
    decimal: 79228162514264337593543950335
    
    Precision Example:
    float (7-digit precision): 1.234568
    double (15-16 digit precision): 1.234567890123456
    decimal (28-29 digit precision): 1.2345678901234567890123456789
    
    

    Explanation:

    • float: A 32-bit floating-point type with approximately 7 digits of precision. It is useful for saving memory in large arrays where precision is not critical.
    • double: A 64-bit floating-point type, offering 15-16 digits of precision. It is the most commonly used floating-point type for scientific and general calculations.
    • decimal: A 128-bit precise decimal type, primarily used for financial and monetary calculations where exactness is critical, as it avoids rounding errors associated with floating-point types.

    Precision Comparison:

    • float will lose precision after 7 digits.
    • double maintains precision up to 15-16 digits.
    • decimal maintains precision up to 28-29 digits, making it ideal for financial calculations where rounding errors can be costly.

    Double

    A double data type is used to work with decimals. In this case, the numbers are whole numbers like 10.11, 20.22 or 30.33. In C#, the datatype is denoted by the keyword "Double". Below is an example of this datatype.

    In our example, we will define a double variable called num. We will then assign a Double value to the variable and then display it accordingly.

    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace DemoApplication
    {
     class Program
     { 
      static void Main(string[] args) 
      {
       double num=30.33;
       Console.Write(num); 
       
       Console.ReadKey();
      }
     }
    }
    

    Code Explanation:-

    1. The double data type is specified to declare a double type variable called num. The variable is then assigned a value of 30.33.
    2. Finally the console.write function is used to display the number to the console.

    If the above code is entered properly and the program is executed successfully, the following output will be displayed.

    Output:

    30.33

    From the output, you can clearly see that the double variable called num was displayed in the console