简体   繁体   English

C#假设浮点文字是double类型?

[英]C# assuming floating point literals are of type double?

I'm following along in my C# textbook and it says that "C# assumes that all floating point literals are of type double." 我正在使用我的C#教科书,它说“C#假定所有浮点文字都是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'." 它回来说:“不能隐式地将类型'double'转换为'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 ';'. 当我试图将整个myTotal行放在括号中并在末尾添加一个F表示它正在寻找一个';'。

So my question is, how do I use float? 所以我的问题是,我如何使用浮动? How can I add an F on the end of this? 如何在此末尾添加F? I've also tried casting (float) in front of it in different ways. 我也试过以不同的方式在它前面铸造(浮动)。

Thank you for your time. 感谢您的时间。

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. C#有一种专门用于表示金融数量的类型:十进制。

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; 此外,在我们使用它时,在将用户输入转换为数字时使用TryParse; users might type something that is not a number, and then you'll get an exception. 用户可能会键入不是数字的内容,然后您将获得异常。

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: 您还可以通过指定浮点文字而不是双精度来阻止自动将值提升为double:

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. ...对于加法运算, int变量将被提升为float ,因此您将使用float而不是double类型作为结果。

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

This should do it. 这应该做到这一点。
Multiplying an int with a float => a float result 将int乘以float => float结果
In your version, int * double (default) => double results => double total. 在您的版本中,int * double(默认值)=> 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). 尝试将其分配给float var,编译器抱怨double to float conversion必须是显式的(精度损失)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM