简体   繁体   English

CodeGo.net>如何处理小数

[英]c# - How to deal with decimal

I have a numericUpDown and set the decimal places on properties to 3 so it became 0.000 我有一个numericUpDown并将属性的小数位数设置为3,所以它变为0.000

Here is the code 这是代码

Decimal inputGrossWeight = numGrossWeight.Value;

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

note: 注意:

  • numGrossWeight is the name of numericUpDown numGrossWeight是numericUpDown的名称

  • grossWeight is the column name on my database grossWeight是数据库中的列名

and I store it to database with the data type float 然后将其存储到数据类型为float的数据库中

So when user type in 2.365 it will stored to database 2.365 too. 因此,当用户输入2.365时,它也将存储到数据库2.365中。

i've tried using many ways and it give me error: 我已经尝试使用多种方法,但它给了我错误:

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'; 类型double的文字不能隐式转换为'decimal'类型; use an 'M' suffix to create a literal of this type 使用“ M”后缀创建此类型的文字

What wrong with my code? 我的代码有什么问题?

You are trying to assign in your if statement. 您正在尝试在if语句中进行赋值。

try this rather 试试这个

if (inputGrossWeight == 0m)

As Jon Skeet mentioned in his comment, by adding the m you are making sure that you are using a decimal literal and thus comparing apples to apples. 正如乔恩·斯基特(Jon Skeet)在评论中提到的那样,通过添加m可以确保您使用的是decimal文字,从而将一个苹果与另一个苹果进行比较。

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

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