Finding the string length of a integer in .NET

后端 未结 6 632
小鲜肉
小鲜肉 2020-12-28 18:12

In .NET what is the best way to find the length of an integer in characters if it was represented as a string?

e.g.

1 = 1 character
10 = 2 characters

6条回答
  •  爱一瞬间的悲伤
    2020-12-28 18:33

    If you want to do it by maths you could try this:

    int integer = 100
    int charCount = (int) Math.Ceiling(Math.Log10(integer+1));
    

    I doubt if this is very much faster than converting to a string though

提交回复
热议问题