Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# assuming floating point literals are of type double?

Tags:

c#

I'm following along in my C# textbook and it says that "C# assumes that all floating point literals are of type double." I get what this is saying, but I'm not quite sure how to apply the fix to the example program I am working on.

using System;
using System.Collections.Generic;
using System.Text;

namespace CoinCounter
{
    class Program
    {
        static void Main(string[] args)
        {
            int quarters;
            int dimes;
            int nickels;
            int pennies;
            float myTotal = 0F;

            Console.Out.WriteLine("How many quarters are in the jar?");
            quarters = int.Parse(Console.In.ReadLine());

            Console.Out.WriteLine("How many dimes are in the jar?");
            dimes = int.Parse(Console.In.ReadLine());

            Console.Out.WriteLine("How many nickels are in the jar?");
            nickels = int.Parse(Console.In.ReadLine());

            Console.Out.WriteLine("How many pennies are in the jar?");
            pennies = int.Parse(Console.In.ReadLine());

            myTotal = (quarters * .25) + (dimes * .10) + (nickels  * .05) + (pennies * .01);
            Console.Out.Write("\nTotal: " + myTotal.ToString("c"));

            Console.In.ReadLine();
        }
    }
}

It comes back saying: "Cannot implicitly convert type 'double' to 'float'." When I try to put that whole myTotal line is parenthesis and add an F to the end is says it's looking for a ';'.

So my question is, how do I use float? How can I add an F on the end of this? I've also tried casting (float) in front of it in different ways.

Thank you for your time.

like image 291
Stradigos Avatar asked Dec 02 '22 06:12

Stradigos


2 Answers

You have a way bigger problem here. You should never represent financial quantities as a float or a double. C# has a type specifically designed to represent financial quantities: decimal.

Your code should be

decimal myTotal = 0.00m;
...
myTotal = (quarters * .25m) + (dimes * .10m) + (nickels  * .05m) + (pennies * .01m);  

The way to remember this: "m is for money".

Also, while we're at it, use TryParse when converting user input to numbers; users might type something that is not a number, and then you'll get an exception.

like image 144
Eric Lippert Avatar answered Dec 04 '22 21:12

Eric Lippert


The language won't implicitly perform the conversion, because it results in a loss of precision. However, you can explicitly perform the conversion using a cast:

myTotal = (float)(
              (quarters * .25)
            + (dimes    * .10)
            + (nickels  * .05)
            + (pennies  * .01)
          );

In this case, you know that you're not dealing with astronomical quantities or exquisitely high precision, so this should be fine.

You could also prevent the automatic promotion of values to double by specifying floating point literals instead of doubles:

myTotal = (quarters * .25F)
        + (dimes    * .10F)
        + (nickels  * .05F)
        + (pennies  * .01F);

...the int variables will be promoted to float for the addition operation, so you'll have a float instead of a double type as the result.

like image 42
Scott Smith Avatar answered Dec 04 '22 22:12

Scott Smith