Convert double to float without Infinity

后端 未结 4 1776
南笙
南笙 2021-01-17 07:26

I\'m converting double to float using ye old float myFloat = (float)myDouble.

This does however sometimes result in \"Infinity\", which is not good for

4条回答
  •  清酒与你
    2021-01-17 08:08

    Use this Function

    public static float DoubleToFloat(double dValue)
        {
            if (float.IsPositiveInfinity(Convert.ToSingle(dValue)))
            {
                return float.MaxValue;
            }
            if (float.IsNegativeInfinity(Convert.ToSingle(dValue)))
            {
                return float.MinValue;
            }
            return Convert.ToSingle(dValue);
        }
    

提交回复
热议问题