Finding the number of places after the decimal point of a Double

后端 未结 8 1164
没有蜡笔的小新
没有蜡笔的小新 2020-12-15 19:39

I have a Double value:

double a = 4.5565;

What is the easiest way to calculate the number of digits after the decimal point (4 in this case

8条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-15 20:26

    Write a function

    int CountDigitsAfterDecimal(double value)
            {
                bool start = false;
                int count = 0;
                foreach (var s in value.ToString())
                {
                    if (s == '.')
                    {
                        start = true;
                    }
                    else if (start)
                    {
                        count++;
                    }
                }
    
                return count;
            }
    

提交回复
热议问题