Compare the sum of double value and sum of decimal value - CSharp Language Basics

CSharp examples for Language Basics:decimal

Description

Compare the sum of double value and sum of decimal value

Demo Code

using System;//from   w  w  w  .  jav a2  s  .c  o m
class Program
{
   static void Main(string[] args)
   {
      Console.WriteLine($"int uses {sizeof(int)} bytes and can store numbers in the range {int.MinValue:N0} to {int.MaxValue:N0}.");
      Console.WriteLine($"double uses {sizeof(double)} bytes and can store numbers in the range {double.MinValue:N0} to {double.MaxValue:N0}.");
      Console.WriteLine($"decimal uses {sizeof(decimal)} bytes and canstore numbers in the range {decimal.MinValue:N0} to {decimal.MaxValue:N0}.");
      double a = 0.1;
      double b = 0.2;
      if (a + b == 0.3)
      {
         Console.WriteLine($"{a} + {b} equals 0.3");
      }
      else
      {
         Console.WriteLine($"{a} + {b} does NOT equal 0.3");
      }
      decimal c = 0.1M; // M indicates a decimal literal value
      decimal d = 0.2M;
      if (c + d == 0.3M)
      {
         Console.WriteLine($"{c} + {d} equals 0.3");
      }
      else
      {
         Console.WriteLine($"{c} + {d} does NOT equal 0.3");
      }
   }
}

Result


Related Tutorials