Lesson 2.12

Double data type C# - programming example


Objective

What is Double data type in c#?
How to declare and initialize
Programming examples
Conversion and Casting

What is Double Data Types in C#?

"Double is 64-bit floating point data type which is derived from System.Double Class. It stores value between ±5.0 × 10−324 to ±1.7 × 10308. It is used for storing very large pointing or decimal type numeric value."

TypeApproximate RangePrecision.Net Framework type
double±5.0 x 10-324 to  ±1.7 x 1030815-16 DigitsSystem.Double

Declaration and Initialization

You can declare double data type as follow:

		  
  double num1=55; //or 
  double num1=55.73; //or
  double num1=55D; //you can suffix D in double value.

		  

Important Notes: A floating-point expression can contain the following sets of values:

1. Positive and negative zero
2. Positive and negative infinity
3. Not-a-Number value (NaN)
4. The finite set of nonzero values

Programming Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace double_data_type
{
    class Program
    {
        static void Main(string[] args)
        {
            double var1, var2, result;            
            var1 = 829.52D;
            var2 = 629.66D;
            result = var1 / var2;            
            Console.WriteLine(result);
            Console.ReadLine();
        }
    }
}
		  

Output

1.3174093955468
_

Conversion

1. Implicit Conversion - No need to convert when left hand side data type 
  is:   double
  
  Example:
  
  double a=5, b=6;
  double result=a+b;

2. Explicit Conversion - You need to convert when left hand side data type is: byte, sbyte, short, ushort, int, uint, long, decimal and char Example: double a=5, b=6; int result=(int)(a+b); Note: When converted double value into other integral type, the point precision gone.

Passing double value as parameter

When calling a function which requires double value in parameter you must cast value into respective datatype to avoid compilation error..

Programming Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace double_data_type
{
    class Program
    {
        static void Main(string[] args)
        {
            double var1=342.52;
            ShowNumber((int)(var1)); // Correct(√)

            ShowNumber(var1); // Incorrect(×)
        }

        public static void ShowNumber(int num1)
        {
            Console.WriteLine(num1);
            Console.ReadLine();
        }
    }
}
		  

Output

342
_

It is best habbit to cast or convert data into respective datatype everywhere. It will save you from unnecessary runtime errors and makes your code more robust.

Explanation

In the above example we have created a function called ShowNumber(int num1) which requires an integer type value as parameter and then display the number.

We have called this function in our main class in which we casted double value into int type before passing.
 double var1=342;
 ShowNumber((int)(var1)); // Correct(√)

 If we don’t cast it will raise compilation error as follow :
 ShowNumber(var1); // Incorrect(×)

 Compilatation Error :Error 1: The best overloaded method match for 'double_data_type.Program.ShowNumber(int)' has some invalid arguments
 Error 2: cannot convert from 'double' to 'int'
  

Summary

Double is 64-bit floating type data type which stores value between ±5.0 x 10-324 to  ±1.7 x 10308. It is mostly used for storing very large decimal value. In the next chapter we will discuss on decimal data type.

BackNext