Get number of digits before decimal point

前端 未结 25 1835
独厮守ぢ
独厮守ぢ 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:59

    I would try this:

    Math.Truncate(467.45).ToString().Length
    

    If you want to be sure not having some weird results for different cultures and with negative decimals, you better use this:

    var myDecimal = 467.45m;
    Math.Truncate(Math.Abs(myDecimal)).ToString(CultureInfo.InvariantCulture).Length
    

提交回复
热议问题