Nullable double NaN comparison in C#

后端 未结 4 1770
独厮守ぢ
独厮守ぢ 2021-01-12 18:03

I have 2 nullable doubles, an expected value and an actual value (let\'s call them value and valueExpected). A percentage is found using 100 * (value / valueExpected). Howev

4条回答
  •  说谎
    说谎 (楼主)
    2021-01-12 18:33

    You can also use

    if (!Double.IsNaN(myDouble ?? 0.0))
    

    The value in the inner-most parenthesis is either the myDouble (with its Nullable<> wrapping removed) if that is non-null, or just 0.0 if myDouble is null. Se ?? Operator (C#).

提交回复
热议问题