C # assuming floaters are of type double?

I follow in my C # tutorial, and he says that "C # assumes that all floating point literals are of type double." I understand what this says, but I'm not quite sure how to apply the fix to the sample program I'm 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(); } } } 

He returns, saying: “It is not possible to implicitly convert the type“ double ”to“ float. ”When I try to put all this myTotal string in a parenthesis and add F to the end, it says it is looking for“; ”.

So my question is: how to use float? How can I add F at the end of this? I also tried casting (float) before it in different ways.

Thank you for your time.

+4
source share
3 answers

The language will not implicitly perform the conversion, because this leads to a loss of accuracy. However, you can explicitly perform the conversion using cast:

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

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

You can also prevent automatic promotion of values ​​twice, by specifying floating-point letters instead of doubling:

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

... int variables will be assigned the float value for the add operation, so as a result you will get a float instead of a double type.

+7
source

You have a big problem here. You must not represent financial values ​​as a float or double . C # has a type specifically designed to represent financial values: decimal.

Your code should be

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

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

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

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

That should do it.
Multiplication int with float => result of float
In your version, int * double (default) => double results => double total.
Try assigning this to float var, the compiler complains that the double-conversion must be explicit (loss of precision).

+1
source

Source: https://habr.com/ru/post/1302326/


All Articles