Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the string length of a integer in .NET

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
99 = 2 characters
100 = 3 characters
1000 = 4 characters

The obvious answer is to convert the int to a string and get its length but I want the best performance possible without the overhead of creating a new string.

like image 662
James Newton-King Avatar asked Mar 24 '10 09:03

James Newton-King


People also ask

Is the length of a string an integer?

Length of a String is nothing but the number of characters that it contains. Java has an inbuilt method called length() to find the number of characters of any String. int length(); where length() is a method to find the number of characters and returns the result as an integer.

What does .length do in C#?

Length returns an integer that represents the number of characters in the string.


5 Answers

you can use logartihms to calculate the length of the int:

public static int IntLength(int i) {
  if (i <= 0) throw new ArgumentOutOfRangeException();

  return (int)Math.Floor(Math.Log10(i)) + 1;
}

the test passes:

[Test]
public void TestIntLength() {
  Assert.AreEqual(1, IntLength(1));
  Assert.AreEqual(1, IntLength(9));
  Assert.AreEqual(2, IntLength(10));
  Assert.AreEqual(2, IntLength(99));
  Assert.AreEqual(3, IntLength(100));
  Assert.AreEqual(3, IntLength(999));
  Assert.AreEqual(4, IntLength(1000));
  Assert.AreEqual(10, IntLength(int.MaxValue));
}

a quick test has shown that the log-method is 4 times faster than the int.ToString().Length method..

the method shown by GvS below (using if-statements) is another 6 times (!) faster than the log method:

public static int IntLengthIf(int i) {
  if (i < 10) return 1;
  if (i < 100) return 2;
  if (i < 1000) return 3;
  if (i < 10000) return 4;
  if (i < 100000) return 5;
  if (i < 1000000) return 6;
  if (i < 10000000) return 7;
  if (i < 100000000) return 8;
  if (i < 1000000000) return 9;
  throw new ArgumentOutOfRangeException();
}

here are the exact timings for the numbers 1 to 10000000:

IntLengthToString: 4205ms
IntLengthLog10: 1122ms
IntLengthIf: 201ms
like image 146
stmax Avatar answered Oct 17 '22 03:10

stmax


If input is in range 0-10000

if (i < 10) return 1;
if (i < 100) return 2;
if (i < 1000) return 3;
if (i < 10000) return 4;
// etc
like image 37
GvS Avatar answered Oct 17 '22 03:10

GvS


You could use something like this:

        int integer = 100;

        int charachtersCount = 0;
        while (integer > 0)
        {
            integer = integer/10;
            charachtersCount++;
        }

But do you really need to optimize this? I would actually prefer using string (looks much better):

integer.ToString().Length
like image 25
Andrew Bezzub Avatar answered Oct 17 '22 03:10

Andrew Bezzub


If you need to deal with negative numbers also, you can take stmax solution with a spin:

public static int IntLength(int i) { 
  if (i == 0) return 1; // no log10(0)
  int n = (i < 0) ? 2 : 1;
  i = (i < 0) ? -i : i;

  return (int)Math.Floor(Math.Log10(i)) + n; 
} 
like image 40
Paulo Santos Avatar answered Oct 17 '22 03:10

Paulo Santos


You can do:

int ndig = 1;
if (n < 0){n = -n; ndig++;}
if (n >= 100000000){n /= 100000000; ndig += 8;}
if (n >=     10000){n /=     10000; ndig += 4;}
if (n >=       100){n /=       100; ndig += 2;}
if (n >=        10){n /=        10; ndig += 1;}

or something along those lines. It takes 4 comparisons and 0-4 divisions.

(On 64 bits you have to add a fifth level.)

like image 29
Mike Dunlavey Avatar answered Oct 17 '22 05:10

Mike Dunlavey