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
110
votes
12 answers

How to Convert Int to Unsigned Byte and Back

I need to convert a number into an unsigned byte. The number is always less than or equal to 255, and so it will fit in one byte. I also need to convert that byte back into that number. How would I do that in Java? I've tried several ways and none…
darksky
  • 20,411
  • 61
  • 165
  • 254
110
votes
7 answers

In java, is it more efficient to use byte or short instead of int and float instead of double?

I've noticed I've always used int and doubles no matter how small or big the number needs to be. So in java, is it more efficient to use byte or short instead of int and float instead of double? So assume I have a program with plenty of ints and…
DisibioAaron
  • 1,342
  • 3
  • 11
  • 17
108
votes
5 answers

Why is Array.Length an int, and not an uint

Why is Array.Length an int, and not a uint. This bothers me (just a bit) because a length value can never be negative. This also forced me to use an int for a length-property on my own class, because when you specify an int-value, this needs to be…
doekman
  • 18,750
  • 20
  • 65
  • 86
106
votes
8 answers

Which is faster : if (bool) or if(int)?

Which value is better to use? Boolean true or Integer 1? The above topic made me do some experiments with bool and int in if condition. So just out of curiosity I wrote this program: int f(int i) { if ( i ) return 99; //if(int) else …
Nawaz
  • 353,942
  • 115
  • 666
  • 851
106
votes
26 answers

Rounding integer division (instead of truncating)

I was curious to know how I can round a number to the nearest whole number. For instance, if I had: int a = 59 / 4; which would be 14.75 if calculated in floating point; how can I store the result as 15 in "a"?
Dave
  • 1,129
  • 2
  • 9
  • 5
105
votes
5 answers

The literal xyz of type int is out of range

I am working with data types at the moment in Java, and if I have understood correctly the type long accepts a value between the ranges of -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807. Now as you can see below, I have create a long…
Mathew Donnan
  • 1,051
  • 2
  • 7
  • 3
104
votes
16 answers

Convert hex string (char []) to int?

I have a char[] that contains a value such as "0x1800785" but the function I want to give the value to requires an int, how can I convert this to an int? I have searched around but cannot find an answer. Thanks.
kizyle502
  • 1,059
  • 2
  • 8
  • 4
103
votes
8 answers

Best way to get whole number part of a Decimal number

What is the best way to return the whole number part of a decimal (in c#)? (This has to work for very large numbers that may not fit into an int). GetIntPart(343564564.4342) >> 343564564 GetIntPart(-323489.32) >> -323489 GetIntPart(324) >> 324 The…
Yaakov Ellis
  • 40,752
  • 27
  • 129
  • 174
102
votes
16 answers

How to check if an integer is within a range of numbers in PHP?

How can I check if a given number is within a range of numbers?
Richard Peers
  • 1,131
  • 3
  • 9
  • 7
99
votes
6 answers

Pandas: Subtracting two date columns and the result being an integer

I have two columns in a Pandas data frame that are dates. I am looking to subtract one column from another and the result being the difference in numbers of days as an integer. A peek at the data: df_test.head(10) Out[20]: First_Date Second…
Kevin
  • 1,659
  • 5
  • 16
  • 22
99
votes
6 answers

How to find max value for Double and Float in Swift

Current learning Swift, there are ways to find max and min value for different kind of Integer like Int.max and Int.min. Is there a way to find max value for Double and Float? Moreover, which document should I refer for this kind of question? I am…
XY L
  • 25,431
  • 14
  • 84
  • 143
99
votes
6 answers

How can I prevent java.lang.NumberFormatException: For input string: "N/A"?

While running my code I am getting a NumberFormatException: java.lang.NumberFormatException: For input string: "N/A" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at…
codemaniac143
  • 1,241
  • 2
  • 11
  • 18
97
votes
2 answers

How to convert char to integer in C?

Possible Duplicates: How to convert a single char into an int Character to integer in C Can any body tell me how to convert a char to int? char c[]={'1',':','3'}; int i=int(c[0]); printf("%d",i); When I try this it gives 49.
Cute
  • 13,643
  • 36
  • 96
  • 112
97
votes
5 answers

c++ parse int from string

Possible Duplicate: How to parse a string to an int in C++? I have done some research and some people say to use atio and others say it's bad, and I can't get it to work anyways. So I just want to ask flat out, whats the right way to convert a…
kralco626
  • 8,456
  • 38
  • 112
  • 169
94
votes
12 answers

How to convert string to integer in C#

How do I convert a string to an integer in C#?
user282078
  • 969
  • 1
  • 6
  • 7