c# - 如何处理小数

标签 c# database winforms numericupdown

我有一个 numericUpDown 并将属性的小数位设置为 3,所以它变成了 0.000

这是代码

Decimal inputGrossWeight = numGrossWeight.Value;

if (inputGrossWeight = 0.000)
{
    MessageBox.Show("Gross Weight must be filled!");
}
else 
{
   Data newData = new Data();
   newData.grossWeight = inputGrossWeight;
}

注意:

  • numGrossWeight 是 numericUpDown 的名称

  • grossWeight 是我数据库中的列名

然后我将其存储到数据类型为 float 的数据库中

所以当用户输入 2.365 时,它也会存储到数据库 2.365。

我试过很多方法,但都出现错误:

Cannot implicitly convert type 'decimal' to 'double'. An explicit conversion exists (are you missing a cast?)

Literal of type double cannot be implicitly converted to type 'decimal'; use an 'M' suffix to create a literal of this type

我的代码有什么问题?

最佳答案

您正试图在您的 if 语句中赋值。

不如试试这个

if (inputGrossWeight == 0m)

正如 Jon Skeet 在他的评论中提到的,通过添加 m,您可以确保您使用的是 decimal 字面值,从而将苹果与苹果进行比较。

关于c# - 如何处理小数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42363019/

相关文章:

c# - Html.BeginForm() 未传递模型

c# - 在等待事件时防止我的程序结束的正确方法是什么

sql - 如何根据表单值连接到指定的数据库表

php - PHP中如何从URL中获取数据库名和表名?

asp.net-mvc - 排除 RDLC 中嵌入的默认字体呈现为 PDF

c# - WinForms 多线程 : Execute a GUI update only if the previous one has finished

c# - GUID 转换和比较与字符串比较的成本有多高

c# - ASP.NET MVC3 C# : Passing query as a parameter and displaying result

java - DateTime 未使用 jsp 插入数据库

c# - 打印导致视觉样式异常