Questions tagged [numbers]

A number is a mathematical object used to count, measure and label. Questions about using numbers in variables processing, numbers processing, conversion, display, logical numerical changes, data type existence, etc. For questions regarding phone numbers, please use tag [phone-number].

Different types of numbers are used in many cases. Numbers can be classified into sets, called number systems.

Natural numbers

The most familiar numbers are the natural numbers or counting numbers (1, 2, 3, ...). Zero is usually not considered a natural number, although definitions vary.

Integers

The integers are formed by the natural numbers (including 0) (0, 1, 2, 3, ...) together with the negatives of the non-zero natural numbers (−1, −2, −3, ...). Viewed as a subset of the real numbers, they are numbers that can be written without a fractional or decimal component, and fall within the set {..., −2, −1, 0, 1, 2, ...}.

Rational numbers

A rational number is a number that can be expressed as a fraction with an integer numerator and a non-zero natural number denominator. Fractions are written as two numbers, the numerator and the denominator, with a dividing bar between them.

A number is rational if (and only if) it can be written as a terminating and/or repeating decimal.

Real numbers

The real numbers include all of the measuring numbers. Real numbers are usually written using decimal numerals, in which a decimal point is placed to the right of the digit with place value one. Each digit to the right of the decimal point has a place value one-tenth of the place value of the digit to its left.

Complex numbers

The complex numbers consist of all numbers of the form

a + b*i

where a and b are real numbers and i is the square root of negative one (the imaginary unit).

More information on Wikipedia

12117 questions
142
votes
25 answers

How to Generate a random number of fixed length using JavaScript?

I'm trying to generate a random number that must have a fixed length of exactly 6 digits. I don't know if JavaScript has given below would ever create a number less than 6 digits? Math.floor((Math.random()*1000000)+1); I found this question and…
hypermiler
  • 1,969
  • 4
  • 19
  • 27
137
votes
38 answers

Create a unique number with javascript time

I need to generate unique id numbers on the fly using javascript. In the past, I've done this by creating a number using time. The number would be made up of the four digit year, two digit month, two digit day, two digit hour, two digit minute, two…
Nick Petrie
  • 5,364
  • 11
  • 41
  • 50
135
votes
3 answers

Format file size as MB, GB, etc

I need to display a file size as a string using sensible units. For example, 1L ==> "1 B"; 1024L ==> "1 KB"; 2537253L ==> "2.3 MB" etc. I found this previous answer, which I didn't find satisfactory. I have come up with my own solution which has…
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
134
votes
6 answers

Non-repetitive random number in numpy

How can I generate non-repetitive random numbers in numpy? list = np.random.random_integers(20,size=(10))
Academia
  • 3,984
  • 6
  • 32
  • 49
134
votes
15 answers

Javascript Thousand Separator / string format

Is there any function in Javascript for formatting number and strings ? I am looking for a way for thousand separator for string or numbers... (Like String.Format In c#)
LostLord
  • 2,279
  • 10
  • 32
  • 32
133
votes
16 answers

Force decimal point instead of comma in HTML5 number input (client-side)

I have seen that some browsers localize the input type="number" notation of numbers. So now, in fields where my application displays longitude and latitude coordinates, I get stuff like "51,983" where it should be "51.982559". My workaround is to…
chocolata
  • 3,258
  • 5
  • 31
  • 60
132
votes
9 answers

How to take off line numbers in Vi?

For displaying line numbers in a file, I use command: set numbers What is the command to clear line numbers from the file?
user710818
  • 23,228
  • 58
  • 149
  • 207
132
votes
9 answers

Why does PHP consider 0 to be equal to a string?

I have the following piece of code: $item['price'] = 0; /* Code to get item information goes in here */ if($item['price'] == 'e') { $item['price'] = -1; } It is intended to initialize the item price to 0 and then get information about it. If…
Sérgio Domingues
  • 1,487
  • 2
  • 10
  • 10
131
votes
17 answers

How do I include negative decimal numbers in this regular expression?

How do I match negative numbers as well by this regular expression? This regex works fine with positive values, but I want it to also allow negative values e.g. -10, -125.5 etc. ^[0-9]\d*(\.\d+)?$ Thanks
user1786107
  • 2,921
  • 5
  • 24
  • 35
126
votes
2 answers

Regex to match a digit two or four times

It's a simple question about regular expressions, but I'm not finding the answer. I want to determine whether a number appears in sequence exactly two or four times. What syntax can I use? \d{what goes here?} I tried \d{2,4}, but this expression…
Renato Dinhani
  • 35,057
  • 55
  • 139
  • 199
121
votes
27 answers

Evaluating a string as a mathematical expression in JavaScript

How do I parse and evaluate a mathematical expression in a string (e.g. '1+1') without invoking eval(string) to yield its numerical value? With that example, I want the function to accept '1+1' and return 2.
wheresrhys
  • 22,558
  • 19
  • 94
  • 162
120
votes
34 answers

Transform numbers to words in lakh / crore system

I'm writing some code that converts a given number into words, here's what I have got after googling. But I think it's a bit too long for such a simple task. Two Regular Expressions and two for loops, I want something simpler. I am trying to achieve…
Jeo
  • 1,211
  • 2
  • 9
  • 6
119
votes
20 answers

Convert long number into abbreviated string in JavaScript, with a special shortness requirement

In JavaScript, how would one write a function that converts a given [edit: positive integer] number (below 100 billion) into a 3-letter abbreviation -- where 0-9 and a-z/A-Z are counting as a letter, but the dot (as it's so tiny in many proportional…
Philipp Lenssen
  • 8,818
  • 13
  • 56
  • 77
118
votes
4 answers

Ruby: Change negative number to positive number?

What's the simplest way of changing a negative number to positive with ruby? ie. Change "-300" to "300"
Shpigford
  • 24,748
  • 58
  • 163
  • 252
116
votes
10 answers

Math - mapping numbers

How do I map numbers, linearly, between a and b to go between c and d. That is, I want numbers between 2 and 6 to map to numbers between 10 and 20... but I need the generalized case. My brain is fried.
Sam
  • 6,167
  • 6
  • 26
  • 24