[Solved] How to format [decimal?] when it’s null? [closed]


You can’t format when it’s null; hopefully the reasons why are obvious. You need to check for the value first:

string formattedValue;
if (partData.FW_Step.HasValue)
    formattedValue = partData.FW_Step.Value.ToString("F3");
else
    formattedValue = "default value for null";

You can make this code shorter using a ternary expression:

string formattedValue = partData.FW_Step.HasValue ? partData.FW_Step.Value.ToString("F3") : "default value for null";

solved How to format [decimal?] when it’s null? [closed]