Questions tagged [integer]

Common datatype in many programming languages for representing both negative and non-negative whole numbers. Use this tag for questions about using, storing, or manipulating integers. For 64-bit integers, use the [long-integer] tag instead.

An integer is a whole number that can be negative, positive, or zero. For example, -2, -1, 0, 1, 2. In many programming languages, the integer data type is commonly represented as 32-bits, but may also be 64-bits (Usually referred to as the Long data type).

For 32-bit integers:

  • The signed range is : -2147483648 to 2147483647.
  • The unsigned range is : 0 to 4294967295.

For 64-bit integers:

  • The signed range is : -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
  • The unsigned range is : 0 to 18,446,744,073,709,551,615.

For further reading, see the Wikipedia article on integers.

13448 questions
783
votes
11 answers

How can I force division to be floating point? Division keeps rounding down to 0?

I have two integer values a and b, but I need their ratio in floating point. I know that a < b and I want to calculate a / b, so if I use integer division I'll always get 0 with a remainder of a. How can I force c to be a floating point number in…
Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319
775
votes
30 answers

How do you round UP a number?

How does one round a number UP in Python? I tried round(number) but it rounds the number down. Here is an example: round(2.3) = 2.0 and not 3, as I would like. Then I tried int(number + .5) but it round the number down again! Example: int(2.3 +…
bodacydo
  • 75,521
  • 93
  • 229
  • 319
770
votes
25 answers

How to concatenate a std::string and an int

I thought this would be really simple, but it's presenting some difficulties. If I have std::string name = "John"; int age = 21; How do I combine them to get a single string "John21"?
Obediah Stane
  • 15,471
  • 14
  • 39
  • 28
744
votes
25 answers

How can I convert a std::string to int?

I want to convert a string to an int and I don't mean ASCII codes. For a quick run-down, we are passed in an equation as a string. We are to break it down, format it correctly and solve the linear equations. Now, in saying that, I'm not able to…
Brandon
  • 7,625
  • 5
  • 19
  • 16
706
votes
23 answers

How can I check if a string represents an int, without using try/except?

Is there any way to tell whether a string represents an integer (e.g., '3', '-17' but not '3.14' or 'asfasfas') Without using a try/except mechanism? is_int('3.14') == False is_int('-7') == True
Adam Matan
  • 128,757
  • 147
  • 397
  • 562
616
votes
15 answers

Converting an integer to a string in PHP

Is there a way to convert an integer to a string in PHP?
kman99
  • 7,315
  • 4
  • 24
  • 20
520
votes
11 answers

Difference between parseInt() and valueOf() in Java?

How is parseInt() different from valueOf() ? They appear to do exactly the same thing to me (also goes for parseFloat(), parseDouble(), parseLong() etc, how are they different from Long.valueOf(string) ? Also, which one of these is preferable and…
Ali
  • 261,656
  • 265
  • 575
  • 769
473
votes
24 answers

Check if a number has a decimal place/is a whole number

I am looking for an easy way in JavaScript to check if a number has a decimal place in it (in order to determine if it is an integer). For instance, 23 -> OK 5 -> OK 3.5 -> not OK 34.345 -> not OK if(number is integer) {...}
Björn
  • 12,587
  • 12
  • 51
  • 70
461
votes
14 answers

How do I convert all strings in a list of lists to integers?

I have a tuple of tuples containing strings: T1 = (('13', '17', '18', '21', '32'), ('07', '11', '13', '14', '28'), ('01', '05', '06', '08', '15', '16')) I want to convert all the string elements into integers and put them back into a…
elfuego1
  • 10,318
  • 9
  • 28
  • 24
461
votes
29 answers

Way to get number of digits in an int?

Is there a neater way for getting the number of digits in an int than this method? int numDigits = String.valueOf(1000).length();
fnst
  • 5,574
  • 4
  • 23
  • 18
445
votes
17 answers

Signed versus Unsigned Integers

Am I correct to say the difference between a signed and unsigned integer is: Unsigned can hold a larger positive value and no negative value. Unsigned uses the leading bit as a part of the value, while the signed version uses the left-most-bit to…
ASDFdotASPX
433
votes
22 answers

Convert boolean result into number/integer

I have a variable that stores false or true, but I need 0 or 1 instead, respectively. How can I do this?
hd.
  • 17,596
  • 46
  • 115
  • 165
416
votes
6 answers

What is the difference between tinyint, smallint, mediumint, bigint and int in MySQL?

What is the difference between tinyint, smallint, mediumint, bigint and int in MySQL? In what cases should these be used?
Sein Kraft
  • 8,417
  • 11
  • 37
  • 39
412
votes
5 answers

How can I convert a character to a integer in Python, and viceversa?

I want to get, given a character, its ASCII value. For example, for the character a, I want to get 97, and vice versa.
Manuel Araoz
  • 15,962
  • 24
  • 71
  • 95
404
votes
10 answers

Declaring an unsigned int in Java

Is there a way to declare an unsigned int in Java? Or the question may be framed as this as well: What is the Java equivalent of unsigned? Just to tell you the context I was looking at Java's implementation of String.hashcode(). I wanted to test the…
Harshdeep
  • 5,614
  • 10
  • 37
  • 45