locked
cast from double to decimal RRS feed

  • Question

  •         This is probably a silly question but i am having trouble casting it keeps telling that it cant perform, I know You cast a value by putting the type you want used in parenthesis just in front of it. For double glassArea to decimal it should be (decimal) glassArea right?

               int windowCost;
                int windowCount;
                int totalCost;
               decimal windowAreaCharge;


                windowCost = 0;
                windowCount = 0;
                totalCost = 0;
                windowAreaCharge = 9.49m;


                double width;
                double height;
                double woodLength;
                double glassArea;
                string widthString;
                string heightString;
                string windowString;


                // read in the width
                Console.WriteLine("Plase enter the width in meters.");
                widthString = Console.ReadLine(); // get the value from the user
                width = double.Parse(widthString); //convert the string into a doubele

                // read in the height
                Console.WriteLine("Please enter the height in meters.");
                heightString = Console.ReadLine();
                height = double.Parse(heightString);

                // calculate (width.I + height) *2*3.25 and convert to feet
                woodLength = 2 * (width + height) * 3.25;
                //calculare width times height
                glassArea = (width * 3.25) * (height * 3.25);

                //print it
                Console.WriteLine("The length of the wood is " + woodLength + " feet ");
                Console.WriteLine("The area of the glass is " + glassArea + " sqaure feet ");

                Console.WriteLine("How many windows do you want?");
                windowString = Console.ReadLine();
                int.Parse(windowString);

                //calculate (area of glass * windowAreaCharge)
                windowCost = glassArea * windowAreaCharge; 

                                               
    Thursday, September 27, 2018 9:11 PM

All replies

  • Hi,

    it's an int. Try this:

    windowCost = (int)(glassArea * (double)windowAreaCharge);

    Greetings, Chris

    Thursday, September 27, 2018 9:22 PM
  • "For double glassArea to decimal it should be (decimal) glassArea right? "

    Yes. C#, like most languages, requires that the operands for a binary operator to be the exact same type. In your case you have a double and decimal. In most cases the compiler can do type coercion to promote a smaller type to a larger type. Hence an int + double = double. However in the case of decimal there is no explicit conversion from double to decimal because of potential data loss issues. You have to make the conversion explicit and a C-style cast works.

    (decimal)glassArea * windowAreaCharge

    Now the result is a decimal.

    But now you have a new compiler error. 

    "Error CS0266 Cannot implicitly convert type 'decimal' to 'int'. An explicit conversion exists (are you missing a cast?)"

    Now you're trying to do this int = decimal

    That isn't allowed either. You're going to lose the decimal portion of the value. You can typecast the decimal to an int if that is what you want. But in this case the correct choice is to probably change the type of windowCost to decimal since it is a price anyway.

    This looks like a programming assignment so it isn't clear how much you've been taught yet but at this point you should understand the basics of variable declarations and scopes. So my recommendation is to stop doing the C-style declaration approach of declaring your variables at the top in a block. It doesn't solve anything and just makes things harder. Instead declare the variables at the point you need them. This would help clarify the problem you're seeing since this wouldn't make sense to you anymore.

    int windowCost = (decimal)glassArea * windowAreaCharge;

    It is obvious now what is going on. Declaring the variables at the point you need them also helps ensure you don't declare variables you never need. Right now you have some strings that you probably won't need in the future. If you declare them at the point you use them and you eliminate the usage later, the declarations go away as well keeping your code cleaner.

    Later on when you are introduced to type inferencing, declaring at the point of usage will also play into that nicely and mostly eliminate the compiler errors you're getting right now as well.


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, September 28, 2018 2:57 PM
  • Hello,

    In addition to guys,

    There are class Convert. You can use - Convert.ToDecimal(expr); - when you need conversion to Decimal.

    The same for other types.


    Sincerely, Highly skilled coding monkey.

    Friday, September 28, 2018 3:12 PM