Paramjit
Paramjit

Reputation: 860

error CS0019: Operator '<=' cannot be applied to operands of type 'double' and 'decimal' in c# console app

I tried to compile the solution multiple times. But it is not compiling when i am using double as variable type. The code is:

using System;
class program
{
    static void Main()
    {
        bool Flag=true;
        string action = "Null";
        double priceGain=0;
        Console.WriteLine("press 'q' or write \"quit\" to exit the application");
        while(Flag==true)
        {
            Console.WriteLine("What is the price Gain? ");
            string input=Console.ReadLine();
            if(double.TryParse(input,out priceGain))
            {
                if (priceGain <= 2m)
                {
                    action ="Sell";
                }
                else if(priceGain > 2m && priceGain <= 3m)
                {
                    action="Do Nothing";
                }
                else
                {
                    action="Buy";
                }
                Console.WriteLine(action);
            }
            else if (input.ToLower()=="q" || input.ToLower() =="quit")
            {
                Environment.Exit(0);
            }
            else
            {
                Console.WriteLine("Invalid Input! please enter a number");
            }
        }
        Console.ReadKey();
    }
}


The error I am getting while compiling is :

error CS0019: Operator '<=' cannot be applied to operands of type 'double' and 'decimal'
error CS0019: Operator '>' cannot be applied to operands of type 'double' and 'decimal'
error CS0019: Operator '<=' cannot be applied to operands of type 'double' and 'decimal'


But when i am using int or decimal instead of double as variable type the progam is compiling and working fine.

Upvotes: 0

Views: 4145

Answers (3)

Ashraf Sada
Ashraf Sada

Reputation: 4905

Change the double to decimal and it should work, like this:

using System;
class program
{
    static void Main()
    {
        bool Flag = true;
        string action = "Null";
        decimal priceGain = 0M;
        Console.WriteLine("press 'q' or write \"quit\" to exit the application");
        while (Flag == true)
        {
            Console.WriteLine("What is the price Gain? ");
            string input = Console.ReadLine();
            if (decimal.TryParse(input, out priceGain))
            {
                if (priceGain <= 2m)
                {
                    action = "Sell";
                }
                else if (priceGain > 2m && priceGain <= 3m)
                {
                    action = "Do Nothing";
                }
                else
                {
                    action = "Buy";
                }
                Console.WriteLine(action);
            }
            else if (input.ToLower() == "q" || input.ToLower() == "quit")
            {
                Environment.Exit(0);
            }
            else
            {
                Console.WriteLine("Invalid Input! please enter a number");
            }
        }
        Console.ReadKey();
    }
}

Upvotes: 1

Patrick Hofman
Patrick Hofman

Reputation: 156978

2m is a decimal since the m is short for decimal. You can use d if you want a double.

if (priceGain <= 2d)

decimal and int can be compared without casting, but not double, as you have experienced.

Note that you might suffer from the imprecision of floating point numbers, so if you can, use decimals.

Upvotes: 2

Luaan
Luaan

Reputation: 63732

M denotes a decimal literal. For double, just use e.g. 2 (or better, 2d) instead of 2M.

Upvotes: 1

Related Questions