Questions tagged [digits]

Questions dealing with getting, manipulating, or changing the digits of numbers

Use this tag for questions dealing with getting, manipulating, or changing the digits of numbers. More about digits on wikipedia

1098 questions
15
votes
5 answers

Is there a more efficient way of splitting a number into its digits?

I have to split a number into its digits in order to display it on an LCD. Right now I use the following method: pos = 7; do { LCD_Display(pos, val % 10); val /= 10; pos--; } while (pos >= 0 && val); The problem with this method is…
alex
  • 3,710
  • 32
  • 44
15
votes
6 answers

How do I generate a random n digit integer in Java using the BigInteger class?

I am unsure about how to generate a random n digit integer in Java using the BigInteger class.
makaveli2178
  • 153
  • 1
  • 1
  • 6
15
votes
4 answers

Android : How to set acceptable numbers and characters in EditText?

I have to set acceptable characters "0123456789" and "semicolon" in the EditText. Below is the code I'm using. android:digits="0123456789;" android:inputType="number|text The problem with that implementation is in HTC phones, semicolon can't be…
iamtheexp01
  • 3,446
  • 9
  • 35
  • 35
14
votes
11 answers

How to extract each digit from a number?

All I can think of is to repeatedly divide the number by 10 (until number is less than 10) and keep a count, but is there a trick for this sort of thing?
ronT
  • 141
  • 1
  • 1
  • 3
14
votes
5 answers

Set edittext using only ",0123456789" programmatically in Android?

My question is this. I can say in xml android:digits="0123456789," But this time I've to it add trough java code. customEditText.setKeyListener(DigitsKeyListener.getInstance("0123456789,")); Doesn't work. Application gives me same result as…
joonasj
  • 551
  • 2
  • 6
  • 19
13
votes
4 answers

sed extract digits

I try to extract digits with sed: echo hgdfjg678gfdg kjg45nn | sed 's/.*\([0-9]\+\).*/\1/g' but result is: 5 How to extract: 678 and 45? Thanks in advance!
Ned
  • 131
  • 1
  • 1
  • 3
13
votes
5 answers

How can I get a one-digit random number in JavaScript?

Possible Duplicate: Generating random whole numbers in JavaScript in a specific range How can I get one-digit random numbers (1, 2, 3, ..., not 0.1, 0.2, ... or 1.0, 5.0, ...) using Math.random() or some other way in JavaScript?
Gowsikan
  • 5,571
  • 8
  • 33
  • 43
13
votes
2 answers

Using Android:Digits attribute to restrict characters stops action next button working

I have a number of EditText fields on an Android app and I've used android:digits to stop invalid characters being input. eg: android:digits="@string/validchars" (where "validchars" is a string resource of all the valid characters - eg A-Z, 0-9, and…
Buzby
  • 573
  • 5
  • 13
12
votes
5 answers

How to allow introducing only digits in jTextField?

I have tried to use the example shown here but java showing error message of "AttributeSet cannot be resolved to a type" That is why I am trying to use another method of allowing only digits: txtUsername.addKeyListener(new…
Bakhtiyor
  • 7,198
  • 15
  • 55
  • 77
12
votes
2 answers

Why `(map digitToInt) . show` is so fast?

Converting non-negative Integer to its list of digits is commonly done like this: import Data.Char digits :: Integer -> [Int] digits = (map digitToInt) . show I was trying to find a more direct way to perform the task, without involving a string…
gawi
  • 13,940
  • 7
  • 42
  • 78
12
votes
5 answers

Count the number of digits in a bash variable

I have a number num=010. I would like to count the number of digits contained in this number. If the number of digits is above a certain number, I would like to do some processing. In the above example, the number of digits is 3. Thanks!
activelearner
  • 7,055
  • 20
  • 53
  • 94
12
votes
10 answers

Sum of digits in a string

if i just read my sum_digits function here, it makes sense in my head but it seems to be producing wrong results. Any tip? def is_a_digit(s): ''' (str) -> bool Precondition: len(s) == 1 Return True iff s is a string containing a single digit…
user1864828
  • 349
  • 1
  • 3
  • 10
11
votes
8 answers

Fastest way to find the largest power of 10 smaller than x

Is there any fast way to find the largest power of 10 smaller than a given number? I'm using this algorithm, at the moment, but something inside myself dies anytime I see it: 10**( int( math.log10(x) ) ) # python pow( 10, (int) log10(x) ) // C I…
peoro
  • 25,562
  • 20
  • 98
  • 150
11
votes
1 answer

Calculating the nth digit of pi using the Bailey–Borwein–Plouffe (BBP) formula

How do I calculate the nth binary (or hexadecimal) digit of pi using the Bailey–Borwein–Plouffe formula? I have been thoroughly searching the Internet and this site for an answer, but I've still yet to find an actual implementation for the…
George
  • 153
  • 1
  • 8
11
votes
3 answers

how can i tell if a string contains ONLY digits and spaces in python using regex

I'm trying to write some script in python (using regex) and I have the following problem: I need to make sure that a string contains ONLY digits and spaces but it could contain any number of numbers. Meaning it should match to the strings: "213"…
Tomer Schweid
  • 325
  • 2
  • 5
  • 18
1 2
3
73 74