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
4073
votes
73 answers

How do I generate random integers within a specific range in Java?

How do I generate a random int value in a specific range? The following methods have bugs related to integer overflow: randomNum = minimum + (int)(Math.random() * maximum); // Bug: `randomNum` can be bigger than `maximum`. Random rn = new…
user42155
  • 48,965
  • 27
  • 59
  • 60
3856
votes
32 answers

How do I cast int to enum in C#?

How do I cast an int to an enum in C#?
lomaxx
  • 113,627
  • 57
  • 144
  • 179
3479
votes
30 answers

How do I convert a String to an int in Java?

How can I convert a String to an int? "1234" → 1234
Unknown user
  • 44,551
  • 16
  • 38
  • 42
2709
votes
33 answers

How do I parse a string to a float or int?

How can I convert a str to float? "545.2222" → 545.2222 How can I convert a str to int? "31" → 31 For the reverse, see Convert integer to string in Python and Converting a float to a string without rounding it. Please instead use…
Tristan Havelick
  • 67,400
  • 20
  • 54
  • 64
2490
votes
40 answers

Generating random whole numbers in JavaScript in a specific range

How can I generate random whole numbers between two specified variables in JavaScript, e.g. x = 4 and y = 8 would output any of 4, 5, 6, 7, 8?
zacharyliu
  • 25,578
  • 4
  • 21
  • 15
2324
votes
32 answers

How to convert a string to an integer in JavaScript

How do I convert a string to an integer in JavaScript?
None
2096
votes
31 answers

How to convert int to string in C++?

How can I convert from int to the equivalent string in C++? I am aware of two methods. Is there another way? (1) int a = 10; char *intStr = itoa(a); string str = string(intStr); (2) int a = 10; stringstream ss; ss << a; string str = ss.str();
Nemo
  • 24,540
  • 12
  • 45
  • 61
1851
votes
22 answers

Generate random integers between 0 and 9

How can I generate random integers between 0 and 9 (inclusive) in Python? For example, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
aneuryzm
  • 63,052
  • 100
  • 273
  • 488
1574
votes
14 answers

Convert integer to string in Python

How do I convert an integer to a string? 42 ⟶ "42" For the reverse, see How do I parse a string to a float or int?. Floats can be handled similarly, but handling the decimal points can be tricky because floating-point values are not precise.…
Hick
  • 35,524
  • 46
  • 151
  • 243
1378
votes
45 answers

What is the maximum value for an int32?

I can never remember the number. I need a memory rule.
Flinkman
  • 17,732
  • 8
  • 32
  • 53
1373
votes
19 answers

Display number with leading zeros

How do I display a leading zero for all numbers with less than two digits? 1 → 01 10 → 10 100 → 100
ashchristopher
  • 25,143
  • 18
  • 48
  • 49
1184
votes
11 answers

Maximum and Minimum values for ints

How do I represent minimum and maximum values for integers in Python? In Java, we have Integer.MIN_VALUE and Integer.MAX_VALUE. See also: What is the maximum float in Python?.
bdhar
  • 21,619
  • 17
  • 70
  • 86
1126
votes
41 answers

Checking whether a variable is an integer or not

How do I check whether a variable is an integer?
Hulk
  • 32,860
  • 62
  • 144
  • 215
847
votes
120 answers

Designing function f(f(n)) == -n

A question I got on my last interview: Design a function f, such that: f(f(n)) == -n Where n is a 32 bit signed integer; you can't use complex numbers arithmetic. If you can't design such a function for the whole range of numbers, design it for…
Hrvoje Prgeša
  • 2,051
  • 5
  • 21
  • 36
796
votes
11 answers

Convert all strings in a list to integers

How do I convert all strings in a list to integers? ['1', '2', '3'] ⟶ [1, 2, 3]
Michael
  • 7,979
  • 3
  • 14
  • 4
1
2 3
99 100