Get number of digits before decimal point

前端 未结 25 2003
独厮守ぢ
独厮守ぢ 2020-12-28 11:53

I have a variable of decimal type and I want to check the number of digits before decimal point in it. What should I do? For example, 467.45 should

25条回答
  •  臣服心动
    2020-12-28 11:57

    The other solutions will lose digits if the number is too large.

    public int Digits(Decimal i)
    {
        NumberFormatInfo format = CultureInfo.CurrentCulture.NumberFormat;
        var str = Math.Abs(i).ToString().Replace(format.NumberGroupSeparator, "");
        var index = str.IndexOf(format.NumberDecimalSeparator);
        var digits = index == -1 ? str.Length : index;
    }
    

提交回复
热议问题