Questions tagged [int]

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

int is a data type that represents an integer (a whole number - not a fraction).

int data types may vary based on:

  • the number of bits used to represent them - 16, 32, etc.
  • whether they allow both negative and positive values (signed integers) or only positive (unsigned integers).

In many (but not all) languages, integers are limited to a specific range due to the way they are stored - for instance, an ISO C 32-bit signed integer can store values from −2,147,483,648 to +2,147,483,647 (-2³¹ to 2³¹-1).

This keyword is often used in C-like languages (including C, C++, Golang, etc.)

9665 questions
153
votes
17 answers

MySQL integer field is returned as string in PHP

I have a table field in a MySQL database: userid INT(11) So I am calling it to my page with this query: "SELECT userid FROM DB WHERE name='john'" Then for handling the result I do: $row=$result->fetch_assoc(); $id=$row['userid']; Now if I…
luca
  • 36,606
  • 27
  • 86
  • 125
150
votes
3 answers

Difference between int32, int, int32_t, int8 and int8_t

I came across the data type int32_t in a C program recently. I know that it stores 32 bits, but don't int and int32 do the same? Also, I want to use char in a program. Can I use int8_t instead? What is the difference? To summarize: what is the…
linuxfreak
  • 2,068
  • 3
  • 20
  • 29
149
votes
5 answers

Converting a double to an int in C#

In our code we have a double that we need to convert to an int. double score = 8.6; int i1 = Convert.ToInt32(score); int i2 = (int)score; Can anyone explain me why i1 != i2? The result that I get is that: i1 = 9 and i2 = 8.
Wouter Dorgelo
  • 11,770
  • 11
  • 62
  • 80
146
votes
8 answers

Why can I pass 1 as a short, but not the int variable i?

Why does the first and second Write work but not the last? Is there a way I can allow all 3 of them and detect if it was 1, (int)1 or i passed in? And really why is one allowed but the last? The second being allowed but not the last really blows my…
user34537
136
votes
8 answers

Rounding a double to turn it into an int (java)

Right now I'm trying this: int a = round(n); where n is a double but it's not working. What am I doing wrong?
David
  • 14,569
  • 34
  • 78
  • 107
136
votes
8 answers

How to check if an int is a null

I have an object called Person. it has several attributes in it; int id; String name; i set a person object like Person p = new Person(1,"Joe");. 1.) I need to check if the object is not null; Is the following expression correct; if (person ==…
Sharon Watinsan
  • 9,620
  • 31
  • 96
  • 140
131
votes
8 answers

C# Float expression: strange behavior when casting the result float to int

I have the following simple code : int speed1 = (int)(6.2f * 10); float tmp = 6.2f * 10; int speed2 = (int)tmp; speed1 and speed2 should have the same value, but in fact, I have : speed1 = 61 speed2 = 62 I know I should probably use Math.Round…
Baalrukh
  • 1,330
  • 2
  • 9
  • 11
130
votes
12 answers

Quickest way to convert a base 10 number to any base in .NET?

I have and old(ish) C# method I wrote that takes a number and converts it to any base: string ConvertToBase(int number, char[] baseChars); It's not all that super speedy and neat. Is there a good, known way of achieving this in .NET? I'm looking…
joshcomley
  • 28,099
  • 24
  • 107
  • 147
126
votes
4 answers

Why is "int i = 2147483647 + 1;" OK, but "byte b = 127 + 1;" is not compilable?

Why is int i = 2147483647 + 1; OK, but byte b = 127 + 1; is not compilable?
goku
  • 1,197
  • 1
  • 10
  • 14
124
votes
8 answers

How to convert an Int to a String of a given length with leading zeros to align?

How can I convert an Int to a 7-character long String, so that 123 is turned into "0000123"?
Ivan
  • 63,011
  • 101
  • 250
  • 382
123
votes
6 answers

ObjectiveC Parse Integer from String

I'm trying to extract a string (which contains an integer) from an array and then use it as an int in a function. I'm trying to convert it to a int using intValue. Here's the code I've been trying. NSArray *_returnedArguments = [serverOutput…
Christian Stewart
  • 15,217
  • 20
  • 82
  • 139
120
votes
8 answers

Why does Decimal.Divide(int, int) work, but not (int / int)?

How come dividing two 32 bit int numbers as ( int / int ) returns to me 0, but if I use Decimal.Divide() I get the correct answer? I'm by no means a c# guy.
user4903
118
votes
3 answers

Why are the fast integer types faster than the other integer types?

In ISO/IEC 9899:2018 (C18), it is stated under 7.20.1.3: 7.20.1.3 Fastest minimum-width integer types 1 Each of the following types designates an integer type that is usually fastest268) to operate with among all integer types that have at least…
116
votes
15 answers

Java, Simplified check if int array contains int

Basically my mate has been saying that I could make my code shorter by using a different way of checking if an int array contains an int, although he won't tell me what it is :P. Current: public boolean contains(final int[] array, final int key) { …
Caleb
  • 1,484
  • 2
  • 12
  • 10
112
votes
11 answers

How to store phone numbers on MySQL databases?

Possible Duplicate: mysql datatype for telephne number and address Any suggestions on best practice to store telephone numbers in a DB? Consider a US phone number: 555 555 1212 555-555-1212 (555) 555 1212 5555551212 1-555-555-1212 1 (555)…
StackOverflowNewbie
  • 39,403
  • 111
  • 277
  • 441