digits
C++ – how to find the length of an integer
Not necessarily the most efficient, but one of the shortest and most readable using C++: std::to_string(num).length()
C: how to break apart a multi digit number into separate variables?
int value = 123; while (value > 0) { int digit = value % 10; // do something with digit value /= 10; }
Removing non numeric characters from a string in Python
The easiest way is with a regexp import re a=”lkdfhisoe78347834 (())&/&745 ” result = re.sub(‘[^0-9]’,”, a) print result >>> ‘78347834745’
How to find out if letter is Alphanumeric or Digit in Swift
For Swift 5 see rustylepord’s answer. Update for Swift 3: let letters = CharacterSet.letters let digits = CharacterSet.decimalDigits var letterCount = 0 var digitCount = 0 for uni in phrase.unicodeScalars { if letters.contains(uni) { letterCount += 1 } else if digits.contains(uni) { digitCount += 1 } } (Previous answer for older Swift versions) A possible … Read more
Why does subtracting ‘0’ in C result in the number that the char is representing?
Because the char are all represented by a number and ‘0’ is the first of them all. On the table below you see that: ‘0’ => 48 ‘1’ => 49 ‘9’ => 57. As a result: (‘9’ – ‘0’) = (57 − 48) = 9 Source: http://www.asciitable.com
Delete digits in Python (Regex)
Add a space before the \d+. >>> s = “This must not b3 delet3d, but the number at the end yes 134411″ >>> s = re.sub(” \d+”, ” “, s) >>> s ‘This must not b3 delet3d, but the number at the end yes ‘ Edit: After looking at the comments, I decided to form … Read more
Gets last digit of a number
Just return (number % 10); i.e. take the modulus. This will be much faster than parsing in and out of a string. If number can be negative then use (Math.abs(number) % 10);
Finding the number of digits of an integer
There’s always this method: n = 1; if ( i >= 100000000 ) { n += 8; i /= 100000000; } if ( i >= 10000 ) { n += 4; i /= 10000; } if ( i >= 100 ) { n += 2; i /= 100; } if ( i >= 10 ) … Read more
How do I separate an integer into separate digits in an array in JavaScript?
Why not just do this? var n = 123456789; var digits = (“”+n).split(“”);