不允许 0.00 位小数 (Visual Studio)

Not allowing 0.00 decimal places ( Visual Studio)

我很困惑为什么说 "literal of type cannot be implicity converted to type 'decimal'; use 'M' suffix. Unsure what use "'M'" 是

        private void btnCalculate_Click(object sender, EventArgs e)
    {
        decimal decTotal = 0.00;
        decimal decTotalAfterDiscount;
        bool bolRadioChecked = false;

        if (chkHygienistTreatment.Checked == true) ;
        {
            decTotal = 119.50;
        }
        if (chkCheckupexam.Checked == true) ;
        {
            decTotal += 100;
        }
        if (chkCrecefilling.Checked == true) ;
        {
            decTotal += 126.30;

您应该像这样更改您的代码:

decimal decTotal = 0.00m;

因为如果你不使用 "m" 编译器不理解它应该被视为小数。这就是 c# 的工作原理。

在 C# 中

0.00

是一个double字面量。不能隐式※转换为 decimal,因为有些值可以在 double 中表示,但在 decimal 中不能表示。

※: 没有显式转换的隐式含义(即 (decimal)0.00)。显式转换可能会抛出。虽然演员不会抛出这个特定的价值。

您可以向文字添加 "M" 或 "m" 以使其成为 decimal 文字:

0.00m

另见 What does the M stand for in C# Decimal literal notation?