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
214
votes
20 answers

How do you append an int to a string in C++?

int i = 4; string text = "Player "; cout << (text + i); I'd like it to print Player 4. The above is obviously wrong but it shows what I'm trying to do here. Is there an easy way to do this or do I have to start adding new includes?
Wawa
205
votes
28 answers

Integer to hex string in C++

How do I convert an integer to a hex string in C++? I can find some ways to do it, but they mostly seem targeted towards C. It doesn't seem there's a native way to do it in C++. It is a pretty simple problem though; I've got an int which I'd like to…
kryptobs2000
  • 3,289
  • 3
  • 27
  • 30
205
votes
9 answers

How to round a Double to the nearest Int in swift?

I'm trying to make a calculator of growth rate (Double) that will round the result to the nearest Integer and recalculate from there, as such: let firstUsers = 10.0 let growth = 0.1 var users = firstUsers var week = 0 while users < 14 { …
duarte harris
  • 2,203
  • 2
  • 11
  • 9
205
votes
19 answers

Converting an int to a binary string representation in Java?

What would be the best way (ideally, simplest) to convert an int to a binary string representation in Java? For example, say the int is 156. The binary string representation of this would be "10011100".
Tyler Treat
  • 14,640
  • 15
  • 80
  • 115
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
195
votes
31 answers

How to check that a string is an int, but not a double, etc.?

PHP has an intval() function that will convert a string to an integer. However I want to check that the string is an integer beforehand, so that I can give a helpful error message to the user if it's wrong. PHP has is_int(), but that returns false…
Amandasaurus
  • 58,203
  • 71
  • 188
  • 248
178
votes
7 answers

Java Round up Any Number

I can't seem to find the answer I'm looking for regarding a simple question: how do I round up any number to the nearest int? For example, whenever the number is 0.2, 0.7, 0.2222, 0.4324, 0.99999 I would want the outcome to be 1. So far I have int b…
Stevanicus
  • 7,561
  • 9
  • 49
  • 70
171
votes
8 answers

Can I convert long to int?

I want to convert long to int. If the value of long > int.MaxValue, I am happy to let it wrap around. What is the best way?
Ian G
  • 29,468
  • 21
  • 78
  • 92
171
votes
19 answers

Convert int to char in java

Below is a code snippet, int a = 1; char b = (char) a; System.out.println(b); But what I get is empty output. int a = '1'; char b = (char) a; System.out.println(b); I will get 1 as my output. Can somebody explain this? And if I want to convert an…
user2640480
  • 1,899
  • 2
  • 13
  • 8
167
votes
1 answer

How do I convert from an integer to a string?

I am unable to compile code that converts a type from an integer to a string. I'm running an example from the Rust for Rubyists tutorial which has various type conversions such as: "Fizz".to_str() and num.to_str() (where num is an integer). I…
user3358302
  • 1,871
  • 2
  • 11
  • 10
163
votes
5 answers

How to convert int to Enum in python?

Using the new Enum feature (via backport enum34) with python 2.7.6. Given the following definition, how can I convert an int to the corresponding Enum value? from enum import Enum class Fruit(Enum): Apple = 4 Orange = 5 Pear = 6 I know…
User
  • 62,498
  • 72
  • 186
  • 247
160
votes
8 answers

Convert bytes to int?

I'm currently working on an encryption/decryption program and I need to be able to convert bytes to an integer. I know that: bytes([3]) = b'\x03' Yet I cannot find out how to do the inverse. What am I doing terribly wrong?
Vladimir Shevyakov
  • 2,511
  • 4
  • 19
  • 40
157
votes
8 answers

swift convert Range to [Int]

how to convert Range to Array I tried: let min = 50 let max = 100 let intArray:[Int] = (min...max) get error Range is not convertible to [Int] I also tried: let intArray:[Int] = [min...max] and let intArray:[Int] = (min...max) as [Int] they…
haitham
  • 3,398
  • 6
  • 21
  • 35
155
votes
7 answers

Converting a column within pandas dataframe from int to string

I have a dataframe in pandas with mixed int and str data columns. I want to concatenate first the columns within the dataframe. To do that I have to convert an int column to str. I've tried to do as follows: mtrx['X.3'] = mtrx.to_string(columns =…
Malfet
  • 1,651
  • 2
  • 11
  • 3
154
votes
5 answers

Why does int i = 1024 * 1024 * 1024 * 1024 compile without error?

The limit of int is from -2147483648 to 2147483647. If I input int i = 2147483648; then Eclipse will prompt a red underline under "2147483648". But if I do this: int i = 1024 * 1024 * 1024 * 1024; it will compile fine. public class Test { …
WUJ
  • 1,663
  • 3
  • 15
  • 21