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
3731
votes
68 answers

How do I generate random integers within a specific range in Java?

How do I generate a random int value in a specific range? I have tried the following, but those do not work: Attempt 1: randomNum = minimum + (int)(Math.random() * maximum); Bug: randomNum can be bigger than maximum. Attempt 2: Random rn = new…
user42155
  • 44,999
  • 25
  • 56
  • 59
2458
votes
28 answers

How do I parse a string to a float or int?

In Python, how can I parse a numeric string like "545.2222" to its corresponding float value, 545.2222? Or parse the string "31" to an integer, 31? I just want to know how to parse a float str to a float, and (separately) an int str to an int.
Tristan Havelick
  • 58,645
  • 19
  • 52
  • 64
2169
votes
39 answers

Generating random whole numbers in JavaScript in a specific range?

How can I generate random whole numbers between two specified variables in JavaScript, e.g. x = 4 and y = 8 would output any of 4, 5, 6, 7, 8?
zacharyliu
  • 22,297
  • 4
  • 16
  • 15
2121
votes
51 answers

How to print a number with commas as thousands separators in JavaScript

I am trying to print an integer in JavaScript with commas as thousands separators. For example, I want to show the number 1234567 as "1,234,567". How would I go about doing this? Here is how I am doing it: function numberWithCommas(x) { x =…
Elias Zamaria
  • 80,938
  • 29
  • 103
  • 136
1911
votes
29 answers

How to convert a string to an integer in JavaScript?

How do I convert a string to an integer in JavaScript?
None
1518
votes
22 answers

Generate random integers between 0 and 9

How can I generate random integers between 0 and 9 (inclusive) in Python? For example, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
aneuryzm
  • 55,858
  • 96
  • 259
  • 471
1452
votes
14 answers

Converting integer to string in Python

I want to convert an integer to a string in Python. I am typecasting it in vain: d = 15 d.str() When I try to convert it to string, it's showing an error like int doesn't have any attribute called str.
Hick
  • 31,852
  • 44
  • 140
  • 235
1380
votes
45 answers

What is the maximum value for an int32?

I can never remember the number. I need a memory rule.
FlinkmanSV
  • 17,702
  • 8
  • 30
  • 53
1076
votes
17 answers

Display number with leading zeros

Given: a = 1 b = 10 c = 100 How do I display a leading zero for all numbers with less than two digits? This is the output I'm expecting: 01 10 100
ashchristopher
  • 21,525
  • 16
  • 44
  • 49
997
votes
41 answers

Checking whether a variable is an integer or not

How do I check whether a variable is an integer?
Hulk
  • 28,804
  • 57
  • 140
  • 210
903
votes
9 answers

Maximum and Minimum values for ints

I am looking for minimum and maximum values for integers in python. For eg., in Java, we have Integer.MIN_VALUE and Integer.MAX_VALUE. Is there something like this in python?
bdhar
  • 17,821
  • 16
  • 62
  • 84
845
votes
118 answers

Designing function f(f(n)) == -n

A question I got on my last interview: Design a function f, such that: f(f(n)) == -n Where n is a 32 bit signed integer; you can't use complex numbers arithmetic. If you can't design such a function for the whole range of numbers, design it for…
Hrvoje Prgeša
  • 2,021
  • 5
  • 21
  • 36
745
votes
11 answers

How can I force division to be floating point? Division keeps rounding down to 0?

I have two integer values a and b, but I need their ratio in floating point. I know that a < b and I want to calculate a / b, so if I use integer division I'll always get 0 with a remainder of a. How can I force c to be a floating point number in…
Nathan Fellman
  • 108,984
  • 95
  • 246
  • 308
701
votes
24 answers

How to concatenate a std::string and an int

I thought this would be really simple, but it's presenting some difficulties. If I have std::string name = "John"; int age = 21; How do I combine them to get a single string "John21"?
Obediah Stane
  • 13,823
  • 14
  • 37
  • 28
582
votes
26 answers

How do you round UP a number in Python?

This problem is killing me. How does one roundup a number UP in Python? I tried round(number) but it round the number down. Example: round(2.3) = 2.0 and not 3, what I would like The I tried int(number + .5) but it round the number down again!…
bodacydo
  • 63,809
  • 83
  • 206
  • 303
1
2 3
99 100