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
274
votes
4 answers

Why does integer division yield a float instead of another integer?

Consider this division in Python 3: >>> 2/2 1.0 Is this intended? I strongly remember earlier versions returning int/int = int. What should I do? Is there a new division operator or must I always cast? In 2.x, the behaviour was indeed reversed;…
Jonas Byström
  • 25,316
  • 23
  • 100
  • 147
269
votes
26 answers

What is the difference between an int and an Integer in Java and C#?

I was reading More Joel on Software when I came across Joel Spolsky saying something about a particular type of programmer knowing the difference between an int and an Integer in Java/C# (Object-Oriented Programming Languages). So, what is the…
CodingWithoutComments
  • 35,598
  • 21
  • 73
  • 86
266
votes
12 answers

How does Java handle integer underflows and overflows and how would you check for it?

How does Java handle integer underflows and overflows? Leading on from that, how would you check/test that this is occurring?
KushalP
  • 10,976
  • 6
  • 34
  • 27
265
votes
22 answers

How to cast an Object to an int

How can I cast an Object to an int in java?
Sheehan Alam
  • 60,111
  • 124
  • 355
  • 556
262
votes
10 answers

From list of integers, get number closest to a given value

Given a list of integers, I want to find which number is the closest to a number I give in input: >>> myList = [4, 1, 88, 44, 3] >>> myNumber = 5 >>> takeClosest(myList, myNumber) ... 4 Is there any quick way to do this?
Ricky Robinson
  • 21,798
  • 42
  • 129
  • 185
261
votes
17 answers

Converting integer to binary in python

In order to convert an integer to a binary, I have used this code : >>> bin(6) '0b110' and when to erase the '0b', I use this : >>> bin(6)[2:] '110' What can I do if I want to show 6 as 00000110 instead of 110?
Smith
  • 2,793
  • 3
  • 16
  • 12
257
votes
35 answers

How to elegantly check if a number is within a range?

How can I do this elegantly with C#? For example, a number can be between 1 and 100. I know a simple if (x >= 1 && x <= 100) would suffice; but with a lot of syntax sugar and new features constantly added to C#/.Net this question is about more…
Sergio Tapia
  • 40,006
  • 76
  • 183
  • 254
231
votes
14 answers

Java integer to byte array

I got an integer: 1695609641 when I use method: String hex = Integer.toHexString(1695609641); system.out.println(hex); gives: 6510f329 but I want a byte array: byte[] bytearray = new byte[] { (byte) 0x65, (byte)0x10, (byte)0xf3,…
stefan
  • 2,313
  • 2
  • 14
  • 4
214
votes
8 answers

Call int() function on every list element?

I have a list with numeric strings, like so: numbers = ['1', '5', '10', '8']; I would like to convert every list element to integer, so it would look like this: numbers = [1, 5, 10, 8]; I could do it using a loop, like so: new_numbers = []; for n…
Silver Light
  • 44,202
  • 36
  • 123
  • 164
212
votes
15 answers

Converting Integer to String with comma for thousands

I want to convert an Integer 35634646 to have the thousand "," so it should be 35,634,646. What would be the quickest way to doing that?
Dominik
  • 4,718
  • 13
  • 44
  • 58
205
votes
14 answers

Converting a string to an integer on Android

How do I convert a string into an integer? I have a textbox I have the user enter a number into: EditText et = (EditText) findViewById(R.id.entry1); String hello = et.getText().toString(); And the value is assigned to the string hello. I want to…
James Andrew
  • 7,197
  • 14
  • 46
  • 62
203
votes
8 answers

Why is 128==128 false but 127==127 is true when comparing Integer wrappers in Java?

class D { public static void main(String args[]) { Integer b2=128; Integer b3=128; System.out.println(b2==b3); } } Output: false class D { public static void main(String args[]) { Integer b2=127; …
vipin k.
  • 2,633
  • 5
  • 22
  • 27
201
votes
6 answers

What is the difference between Int and Integer?

In Haskell, what is the difference between an Int and an Integer? Where is the answer documented?
0xAX
  • 20,957
  • 26
  • 117
  • 206
198
votes
5 answers

Integer.valueOf() vs. Integer.parseInt()

Aside from Integer.parseInt() handling the minus sign (as documented), are there any other differences between Integer.valueOf() and Integer.parseInt()? And since neither can parse , as a decimal thousands separator (produces NumberFormatException),…
ateiob
  • 9,016
  • 10
  • 44
  • 55
197
votes
3 answers

How to convert integer to string in C?

I tried this example: /* itoa example */ #include #include int main () { int i; char buffer [33]; printf ("Enter a number: "); scanf ("%d",&i); itoa (i,buffer,10); printf ("decimal: %s\n",buffer); …
Janko
  • 8,985
  • 7
  • 34
  • 51