Get number of digits before decimal point

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

    This will do if you really don't want to use the Log method (which IMO is the best way). It's about the clearest way I can think of of doing this using ToString():

    Math.Abs(val).ToString("f0", CultureInfo.InvariantCulture).Length
    

    Or alternatively, if you don't want to count 0.123M as having one digit:

    Math.Abs(val).ToString("#", CultureInfo.InvariantCulture).Length
    

提交回复
热议问题