Questions tagged [integer]

Common datatype in many programming languages for representing a whole number. Use this tag for questions about using, storing, or manipulating integers.

An integer is a whole number that can be negative, positive, or zero. (e.g. ... -2, -1, 0, 1, 2 ...) Use this tag for questions about using, storing, or manipulating integers.

For further reading, here is a Wikipedia article on integers.

11160 questions
291
votes
14 answers

What's the most efficient way to test two integer ranges for overlap?

Given two inclusive integer ranges [x1:x2] and [y1:y2], where x1 ≤ x2 and y1 ≤ y2, what is the most efficient way to test whether there is any overlap of the two ranges? A simple implementation is as follows: bool testOverlap(int x1, int x2, int y1,…
WilliamKF
  • 36,283
  • 61
  • 170
  • 271
283
votes
10 answers

How do I work around JavaScript's parseInt octal behavior?

Try executing the following in JavaScript: parseInt('01'); //equals 1 parseInt('02'); //equals 2 parseInt('03'); //equals 3 parseInt('04'); //equals 4 parseInt('05'); //equals 5 parseInt('06'); //equals 6 parseInt('07'); //equals 7 parseInt('08');…
Portman
  • 30,925
  • 24
  • 79
  • 101
283
votes
26 answers

How to find length of digits in an integer?

In Python, how do you find the number of digits in an integer?
Strigoides
  • 3,439
  • 5
  • 19
  • 25
282
votes
10 answers

How to convert an int to string in C?

How do you convert an int (integer) to a string? I'm trying to make a function that converts the data of a struct into a string to save it in a file.
user1063999
  • 2,851
  • 2
  • 12
  • 5
272
votes
8 answers

Convert floats to ints in Pandas?

I've been working with data imported from a CSV. Pandas changed some columns to float, so now the numbers in these columns get displayed as floating points! However, I need them to be displayed as integers, or, without comma. Is there a way to…
MJP
  • 3,827
  • 5
  • 14
  • 16
269
votes
15 answers

Convert array values from string to int?

The following code: $string = "1,2,3" $ids = explode(',', $string); var_dump($ids); Returns the array: array(3) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" } I need for the values to be of type int instead of type…
cwal
  • 3,272
  • 3
  • 17
  • 20
269
votes
27 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
  • 33,560
  • 21
  • 70
  • 85
253
votes
19 answers

Mapping two integers to one, in a unique and deterministic way

Imagine two positive integers A and B. I want to combine these two into a single integer C. There can be no other integers D and E which combine to C. So combining them with the addition operator doesn't work. Eg 30 + 10 = 40 = 40 + 0 = 39 +…
harm
  • 9,001
  • 9
  • 34
  • 39
250
votes
10 answers

How to properly compare two Integers in Java?

I know that if you compare a boxed primitive Integer with a constant such as: Integer a = 4; if (a < 5) a will automatically be unboxed and the comparison will work. However, what happens when you are comparing two boxed Integers and want to…
Samatha84
246
votes
10 answers

Safest way to convert float to integer in python?

Python's math module contain handy functions like floor & ceil. These functions take a floating point number and return the nearest integer below or above it. However these functions return the answer as a floating point number. For example: import…
Boaz
  • 21,983
  • 21
  • 65
  • 76
242
votes
5 answers

Python integer division yields float

Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> 2/2 1.0 Is this intended? I strongly remember earlier versions returning int/int=int? What…
Jonas Byström
  • 22,233
  • 21
  • 93
  • 137
240
votes
19 answers

How to cast an Object to an int

How can I cast an Object to an int in java?
Sheehan Alam
  • 57,155
  • 123
  • 348
  • 546
236
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,172
  • 6
  • 30
  • 27
208
votes
14 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,263
  • 3
  • 14
  • 12
205
votes
13 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,053
  • 2
  • 13
  • 4