Reputation: 864
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.
Upvotes: 4
Views: 1102
Reputation: 660004
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.
Upvotes: 11
Reputation: 136613
myTotal = (quarters * .25f) + (dimes * .10f) + (nickels * .05f) + (pennies * .01f);
This should do it.
Multiplying an int with a float => a float result
In your version, int * double (default) => double results => double total.
Try to assign that to a float var, the compiler complains that double to float conversion has to be explicit (loss of precision).
Upvotes: 1
Reputation: 3986
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.
Upvotes: 7