Maybe something like this will help ...
@Html.DisplayFor(modelItem => item.FoodAmount % 1 == 0 ? item.FoodAmount.ToString("F0") : item.FoodAmount.ToString("F2"))
This should provide the following results:
FoodAmount | Output -------------------- 3.00 | 3 3.01 | 3.01 3.5 | 3.50
I deliberately settled this on one line according to your Html Helper code, but you can create a helper function / extension method for it if you prefer.
The main idea is to get the remainder of dividing by 1. If the result is zero (that is, it can be divided by 1 without a remainder), then it can be considered an integer
Alternative : it will also be an opportunity and can improve reading ...
item.FoodAmount.ToString("F2").TrimEnd('0', '.')
It may even give you better results ...
FoodAmount | Output -------------------- 3.00 | 3 3.01 | 3.01 3.5 | 3.5
source share