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
38
votes
5 answers

How to find out if letter is Alphanumeric or Digit in Swift

I want to count the number of letters, digits and special characters in the following string: let phrase = "The final score was 32-31!" I tried: for tempChar in phrase { if (tempChar >= "a" && tempChar <= "z") { letterCounter++ } //…
sirab333
  • 3,662
  • 8
  • 41
  • 54
38
votes
8 answers

Why does subtracting '0' in C result in the number that the char is representing?

Can someone explain why this works? char c = '9'; int x = (int)(c - '0'); Why does subtracting '0' from an ascii code of a char result the number that that char is representing?
Silviu.
  • 525
  • 1
  • 4
  • 13
31
votes
11 answers

C: how to break apart a multi digit number into separate variables?

Say I have a multi-digit integer in C. I want to break it up into single-digit integers. 123 would turn into 1, 2, and 3. How can I do this, especially if I don't know how many digits the integer has?
mugetsu
  • 4,228
  • 9
  • 50
  • 79
31
votes
17 answers

C++ - how to find the length of an integer

I'm trying to find a way to find the length of an integer (number of digits) and then place it in an integer array. The assignment also calls for doing this without the use of classes from the STL, although the program spec does say we can use…
user1888527
  • 543
  • 1
  • 7
  • 12
30
votes
4 answers

Function to count number of digits in string

I was looking for a quick PHP function that, given a string, would count the number of numerical characters (i.e. digits) in that string. I couldn't find one, is there a function to do this?
Ashley Strout
  • 6,107
  • 5
  • 25
  • 45
29
votes
3 answers

Ruby: How to find out if a character is a letter or a digit?

I just started tinkering with Ruby earlier this week and I've run into something that I don't quite know how to code. I'm converting a scanner that was written in Java into Ruby for a class assignment, and I've gotten down to this section: if…
Cory Regan
  • 303
  • 1
  • 4
  • 5
28
votes
6 answers

C# string starts with a number regex

I've been searching around for a little while to figure out how to confirm a string begins with a number. I came across a regex statement '/^\d/' which I can confirm says if starts with digit. However I can not seem to get it to work in the code…
toosweetnitemare
  • 2,226
  • 8
  • 33
  • 44
26
votes
8 answers

Sum all the digits of a number Javascript

I am newbie. I want to make small app which will calculate the sum of all the digits of a number. For example, if I have the number 2568, the app will calculate 2+5+6+8 which is equal with 21. Finally, it will calculate the sum of 21's digits and…
Hodorogea Alexandru
  • 449
  • 2
  • 5
  • 11
22
votes
1 answer

Are the character digits ['0'..'9'] required to have contiguous numeric values?

Must a C++ implementation set the chars '0'-'9' to have contiguous numeric values, i.e. so that: '0' -> 0+n '1' -> 1+n m -> m+n '9' -> 9+n I cannot find it mentioned in the documentation of isdigit ([classification] (22.3.3.1 Character…
Sebastian Mach
  • 38,570
  • 8
  • 95
  • 130
22
votes
4 answers

One or two numeric digits Regex

I have the below code. It works only when I have 2 digits. If I have 1 digit doesn't work. I want to work in both cases: one or two digit. var numberRegex = /^[1-9][0-9]$/; I've tried something like this but unfortunately doesn't work: var…
CBuzatu
  • 745
  • 5
  • 16
  • 29
20
votes
1 answer

add leading zeros to a list of numbers in Python

I am new to Python. I am trying to adjust the format of a list which looks like below: data=[1,10,313,4000,51234,123456] and I would like to convert them to a list of strings with leading…
user2854008
  • 1,161
  • 4
  • 17
  • 23
18
votes
7 answers

Set the digits after decimal point

I have a float number for example 12.12123 Is there a function which would display only number with 2 digits after decimal point 12.12 ? Here is the code: y1 = ( c1 - (a1 * x)) / b1; y2 = ( c2 - a2 * x) / b2; if (y1 == y2) cout << "The…
user474401
  • 181
  • 1
  • 1
  • 4
18
votes
6 answers

Convert integer number to three digit

I have an integer variable .if its 1-9 it only displays as "1" or "9", I'm looking to convert the variable to save as 3 digits, ie. "001", or "009", etc. any ideas? I am using C#,ASP.Net
user1270940
  • 221
  • 1
  • 5
  • 9
17
votes
1 answer

How do I split an integer into individual digits?

I'm writing a function that requires the individual digits of a larger integer to perform operations on. I've tried the following: fn example(num: i32) { // I can safely unwrap because I know the chars of the string are going to be valid …
Electric Coffee
  • 11,733
  • 9
  • 70
  • 131
16
votes
6 answers

JavaScript format number to day with always 3 digits

Possible Duplicate: How can I create a Zerofilled value using JavaScript? I have to output a day number that must always have 3 digits. Instead of 3 it must write 003, instead of 12 it must write 012. If it is greater than 100 output it without…
ali
  • 10,927
  • 20
  • 89
  • 138
1
2
3
73 74