Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get number of digits in an unsigned long integer c#

I'm trying to determine the number of digits in a c# ulong number, i'm trying to do so using some math logic rather than using ToString().Length. I have not benchmarked the 2 approaches but have seen other posts about using System.Math.Floor(System.Math.Log10(number)) + 1 to determine the number of digits. Seems to work fine until i transition from 999999999999997 to 999999999999998 at which point, it i start getting an incorrect count.

Has anyone encountered this issue before ?

I have seen similar posts with a Java emphasis @ Why log(1000)/log(10) isn't the same as log10(1000)? and also a post @ How to get the separate digits of an int number? which indicates how i could possibly achieve the same using the % operator but with a lot more code

Here is the code i used to simulate this

Action<ulong> displayInfo = number => 
 Console.WriteLine("{0,-20} {1,-20} {2,-20} {3,-20} {4,-20}", 
  number, 
  number.ToString().Length, 
  System.Math.Log10(number), 
  System.Math.Floor(System.Math.Log10(number)),
  System.Math.Floor(System.Math.Log10(number)) + 1);

Array.ForEach(new ulong[] {
 9U,
 99U,
 999U,
 9999U,
 99999U,
 999999U,
 9999999U,
 99999999U,
 999999999U,
 9999999999U,
 99999999999U,
 999999999999U,
 9999999999999U,
 99999999999999U,
 999999999999999U,
 9999999999999999U,
 99999999999999999U,
 999999999999999999U,
 9999999999999999999U}, displayInfo);

Array.ForEach(new ulong[] {
 1U,
 19U,
 199U,
 1999U,
 19999U,
 199999U,
 1999999U,
 19999999U,
 199999999U,
 1999999999U,
 19999999999U,
 199999999999U,
 1999999999999U,
 19999999999999U,
 199999999999999U,
 1999999999999999U,
 19999999999999999U,
 199999999999999999U,
 1999999999999999999U
}, displayInfo);

Thanks in advance

Pat

like image 824
pmcgrath Avatar asked Feb 09 '11 19:02

pmcgrath


People also ask

How do you find the number of digits in an integer?

The formula will be integer of (log10(number) + 1). For an example, if the number is 1245, then it is above 1000, and below 10000, so the log value will be in range 3 < log10(1245) < 4. Now taking the integer, it will be 3. Then add 1 with it to get number of digits.

How do you check if a number contains a certain digit in C?

bool containsDigit(int number, int digit) { int a(0),b; b=number; while(number>0) { a=a+1; number=number/10; } cout<<a; while(a>1) { a=a-1; if(b/pow(10,a)==digit) {cout<<"true\n"; break;} else { if(a==1) cout<<"false\n"; else cout<<""; } b=b-(b/pow(10,a))*pow(10,a); } } I don't know why I always get false when I write ...

Can unsigned long be zero?

Unsigned long variables are extended size variables for number storage, and store 32 bits (4 bytes). Unlike standard longs unsigned longs won't store negative numbers, making their range from 0 to 4,294,967,295 (2^32 - 1).


1 Answers

log10 is going to involve floating point conversion - hence the rounding error. The error is pretty small for a double, but is a big deal for an exact integer!

Excluding the .ToString() method and a floating point method, then yes I think you are going to have to use an iterative method but I would use an integer divide rather than a modulo.

Integer divide by 10. Is the result>0? If so iterate around. If not, stop. The number of digits is the number of iterations required.

Eg. 5 -> 0; 1 iteration = 1 digit.

1234 -> 123 -> 12 -> 1 -> 0; 4 iterations = 4 digits.

like image 193
winwaed Avatar answered Sep 29 '22 10:09

winwaed