0

I have some decimal input from my DataGridView; let's say 4,3.

I want to compare if it is between 3,5 and 4,4.

How should I write the numbers 3,5 and 4,4 in my code?

With my current attempt, I get some errors like

; expected

} expected

Operator || cannot be applied

I tried with Cultureinfo but I do not know how exactly use it.

Here is my code so far:

var updateLabStar = db.PatientLab.Where(x => x.BloodUrinId == item.BloodUrinId).FirstOrDefault();

if (Convert.ToDecimal(Result) < 3,5 || Convert.ToDecimal(Result) > 4,4)
{
    updateLabStar.Interval = updateLabStar.Result + " *";
    db.SaveChanges();
}
dbc
  • 104,963
  • 20
  • 228
  • 340
Helen Tekie
  • 515
  • 1
  • 6
  • 23
  • 1
    You can't use a comma as a decimal separator for literal values in your code, you must use a period. The `CultureInfo` only affects how the decimal is displayed, not how it is stored or compared. If you want to use a comma for literals in your code, you will need to define them as strings then parse them into decimals using the appropriate culture. – D M Jan 03 '22 at 16:43
  • @DM , thank you for your response. I have tried by many way like string.Format(..., "{0:N}", parsed)and I'am sure I'am doing in wrong way. Would you please show me how do you mean? Thank you in advance – Helen Tekie Jan 03 '22 at 18:12
  • @DM, I disagree that "`CultureInfo` only affects how the decimal is displayed" because it implements the `IFromatProvider` interface which can be used to parse data for example. – Qwertyluk Jan 03 '22 at 18:21
  • @HelenTekie Sure, take a look at [this fiddle](https://dotnetfiddle.net/qSHyEh). – D M Jan 03 '22 at 19:02
  • @Qwertyluk valid point, but the representation once it's parsed is identical to any other culture. That's what I was getting at. It doesn't make sense to have a culture once you have a `decimal` because C# only handles a `decimal` one way. – D M Jan 03 '22 at 19:04
  • @DM, Thank you my dear. It's working now perfectly. Thank you again! – Helen Tekie Jan 04 '22 at 00:27

1 Answers1

0

How to write decimal literal.

var updateLabStar = db.PatientLab.Where(x => x.BloodUrinId == item.BloodUrinId).FirstOrDefault();

if (Convert.ToDecimal(Result) < 3.5m || Convert.ToDecimal(Result) > 4.4m)
{
    updateLabStar.Interval = updateLabStar.Result + " *";
    db.SaveChanges();
}
Backs
  • 24,430
  • 5
  • 58
  • 85