Search code examples
c#databasewinformsnumericupdown

c# - How to deal with decimal


I have a numericUpDown and set the decimal places on properties to 3 so it became 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

  • grossWeight is the column name on my database

and I store it to database with the data type float

So when user type in 2.365 it will stored to database 2.365 too.

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'; use an 'M' suffix to create a literal of this type

What wrong with my code?


Solution

  • You are trying to assign in your if statement.

    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.