Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operator '<' cannot be applied to operands of type 'decimal' and 'double'

I'm trying to come up with a program that calculates grades given from the users input. I am also trying to set a limit on how high or low the user input can be (i.e 0 <= or >= 100). But when I use decimal it keeps giving me this error, "Operator '<' cannot be applied to operands of type 'decimal' and 'double'"

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

namespace Grade_Program
{
    class Program
    {
        static void Main(string[] args)
        {
            string First;
            string Last;
            First = "Cristiano";
            Last = " Ronaldo";
            Console.Write("Please enter student name <First Last>: ");
            Console.WriteLine(First + Last );

            Console.WriteLine(" ");

                                                     Console.WriteLine("*************NOTE**********************************************");
        Console.WriteLine("*** Be sure to include decimal point for scores.            ***");
        Console.WriteLine("***     !!!All score should range from 0.00 to 100.00 !!    ***");
        Console.WriteLine("***                                                         ***");
        Console.WriteLine("*** For example : 80.50                                     ***");
        Console.WriteLine("***************************************************************");

        Console.WriteLine(" ");

        decimal Exam_1;
        decimal Exam_2;
        decimal Exam_3;
        decimal Assignment_1;
        decimal Assignment_2;

        Console.Write("Please enter score for Exam 1 <Example: 100.0>: ");
        Exam_1 = Convert.ToDecimal(Console.ReadLine());

        if (Exam_1 < 0.0 | Exam_1 > 100.0)
            Console.Write("Exam score cannot be less than 0. or greater than                      100.0. Please re-enter the score for Exam 1 <Example: 95.0>:");
            Exam_1 = Convert.ToDecimal(Console.ReadLine());

        Console.Write("Please enter score for Exam 2 <Example: 0.0>: ");
        Exam_2 = Convert.ToDecimal(Console.ReadLine());
like image 991
Abdulhamid Avatar asked Mar 08 '16 03:03

Abdulhamid


People also ask

Can you multiply a double by a decimal in C#?

You can't multiply a decimal by a double . You can fix this by type casting, but you probably just want to stick with using decimal for all prices and VAT rates throughout.

What is the difference between double and decimal?

Double uses 64 bits to represent data. Decimal uses 128 bits to represent data.

How do you convert decimal to double?

You can also convert a Decimal to a Double value by using the Explicit assignment operator. Because the conversion can entail a loss of precision, you must use a casting operator in C# or a conversion function in Visual Basic.

What does M mean in decimal C#?

From the C# Annotated Standard (the ECMA version, not the MS version): The decimal suffix is M/m since D/d was already taken by double . Although it has been suggested that M stands for money, Peter Golde recalls that M was chosen simply as the next best letter in decimal .


3 Answers

There are at least four issues I noticed in your code.

Firstly, as mentioned, you should use M suffix to tell the C# compiler that it is a decimal for accepted comparison:

if (Exam_1 < 0.0M | Exam_1 > 100.0M)

But secondly, use || instead of |, because you want to do OR operation, not Bitwise-OR

if (Exam_1 < 0.0M || Exam_1 > 100.0M) //change | to ||

And thirdly, I think quite important for you to know this: you wouldn't need decimal data type for exam mark (unless your exam mark can be of format 99.12345678901234556789012345 - which is quite impossible).

decimal is normally used for numbers requiring very high precision (such as money calculation in the bank) up to more than 16-digit accuracy. If your exam mark does not need that, don't use decimal, it is overkill. Just use double or int or float for your Exams and you are most probably in the right track.

Fourthly, about your error handling, this is incorrect way of doing it:

if (Exam_1 < 0.0 | Exam_1 > 100.0)
    Console.Write("Exam score cannot be less than 0. or greater than                      100.0. Please re-enter the score for Exam 1 <Example: 95.0>:");
    Exam_1 = Convert.ToDecimal(Console.ReadLine());

due to two reasons:

  1. Your Exam_1 is outside of the block (there isn't {} bracket)
  2. You use if while you should use while

This is the right way to do it:

double Exam_1 = -1; //I use double to simplify

Console.Write("Please enter score for Exam 1 <Example: 100.0>: ");
Exam_1 = Convert.ToDouble(Console.ReadLine());

while (Exam_1 < 0.0 || Exam_1 > 100.0) { //see the curly bracket
    Console.Write("Exam score cannot be less than 0. or greater than                      100.0. Please re-enter the score for Exam 1 <Example: 95.0>:");
    Exam_1 = Convert.ToDouble(Console.ReadLine());
} //see the end curly bracket

In C# language, indentation does not mean scoping, unlike in language like Python.

like image 97
Ian Avatar answered Oct 24 '22 06:10

Ian


For decimal you have to add "M" suffix to the value to tell the computer it is a decimal. Otherwise computer will consider it as a double.

yourDecimal < 98.56M;

like image 27
Isuru Rangana Avatar answered Oct 24 '22 07:10

Isuru Rangana


As other's have already pointed out. In order to compare a decimal type using the greater than or less than operators you must compare it to another decimal type. In order to declare a literal number as a decimal it requires the M or m suffix. Here is the MSDN on the decimal type for reference.

if (Exam_1 < 0.0m || Exam_1 > 100.0m)

Here's a .NET fiddle with the fix.

like image 1
David Pine Avatar answered Oct 24 '22 07:10

David Pine