Get number of digits before decimal point

前端 未结 25 1934
独厮守ぢ
独厮守ぢ 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 12:11

    You could use ToString function with a custom format.

    Decimal value = 467.45m;
    int count = Math.Abs(value).ToString("#", System.Globalization.CultureInfo.InvariantCulture).Length;
    

    The # specify you only want the characters before the .

    The System.Globalization.CultureInfo.InvariantCulture ensure you won't get any formating from the Region Option.

提交回复
热议问题