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

10902 questions
2458
votes
49 answers

Validate decimal numbers in JavaScript - IsNumeric()

What's the cleanest, most effective way to validate decimal numbers in JavaScript? Bonus points for: Clarity. Solution should be clean and simple. Cross-platform. Test cases: 01. IsNumeric('-1') => true 02. IsNumeric('-1.5') => true 03.…
Michael Haren
  • 97,268
  • 39
  • 159
  • 200
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
1092
votes
19 answers

Can I hide the HTML5 number input’s spin box?

Is there a consistent way across browsers to hide the new spin boxes that some browsers (such as Chrome) render for HTML input of type number? I am looking for a CSS or JavaScript method to prevent the up/down arrows from appearing.
Alan
  • 11,144
  • 3
  • 15
  • 13
1020
votes
29 answers

How to sort an array of integers correctly

Trying to get the highest and lowest value from an array that I know will contain only integers seems to be harder than I thought. var numArray = [140000, 104, 99]; numArray = numArray.sort(); console.log(numArray) I'd expect this to show 99,…
peirix
  • 32,055
  • 22
  • 90
  • 125
762
votes
46 answers

How do I check that a number is float or integer?

How to find that a number is float or integer? 1.25 --> float 1 --> integer 0 --> integer 0.25 --> float
coure2011
  • 34,326
  • 71
  • 200
  • 319
657
votes
12 answers

Formatting a number with leading zeros in PHP

I have a variable which contains the value 1234567. I would like it to contain exactly 8 digits, i.e. 01234567. Is there a PHP function for that?
Andromeda
  • 11,531
  • 18
  • 68
  • 98
637
votes
24 answers

Show a number to two decimal places

What's the correct way to round a PHP string to two decimal places? $number = "520"; // It's a string from a database $formatted_number = round_to_2dp($number); echo $formatted_number; The output should be 520.00; How should the round_to_2dp()…
Rich Bradshaw
  • 67,265
  • 44
  • 170
  • 236
600
votes
23 answers

What's the best way to convert a number to a string in JavaScript?

What's the "best" way to convert a number to a string (in terms of speed advantage, clarity advantage, memory advantage, etc) ? Some examples: String(n) n.toString() ""+n n+""
Pacerier
  • 76,400
  • 86
  • 326
  • 602
516
votes
17 answers

How to extract numbers from a string in Python?

I would extract all the numbers contained in a string. Which is the better suited for the purpose, regular expressions or the isdigit() method? Example: line = "hello 12 hi 89" Result: [12, 89]
pablouche
  • 5,211
  • 3
  • 14
  • 6
489
votes
13 answers

How to zero pad a sequence of integers in bash so that all have the same width?

I need to loop some values, for i in $(seq $first $last) do does something here done For $first and $last, i need it to be of fixed length 5. So if the input is 1, i need to add zeros in front such that it becomes 00001. It loops till 99999 for…
John Marston
  • 9,869
  • 6
  • 20
  • 17
445
votes
7 answers

Java - Convert integer to string

Given a number: int number = 1234; Which would be the "best" way to convert this to a string: String stringNumber = "1234"; I have tried searching (googling) for an answer but no many seemed "trustworthy".
Trufa
  • 35,711
  • 41
  • 118
  • 180
417
votes
23 answers

How can I extract a number from a string in JavaScript?

I have a string in JavaScript (e.g #box2) and I just want the 2 from it. I tried: var thestring = $(this).attr('href'); var thenum = thestring.replace( /(^.+)(\w\d+\w)(.+$)/i,'$2'); alert(thenum); It still returns #box2 in the alert, how can I get…
user1022585
  • 11,163
  • 18
  • 52
  • 72
397
votes
28 answers

How can I limit possible inputs in a HTML5 "number" element?

For element, maxlength is not working. How can I restrict the maxlength for that number element?
Prasad
  • 56,343
  • 61
  • 142
  • 199
396
votes
16 answers

Check if string contains only digits

I want to check if a string contains only digits. I used this: var isANumber = isNaN(theValue) === false; if (isANumber){ .. } But realized that it also allows + and -. Basically, I want to make sure an input contains ONLY digits and no other…
patad
  • 7,674
  • 10
  • 33
  • 43
384
votes
8 answers

How to format a floating number to fixed width in Python

How do I format a floating number to a fixed width with the following requirements: Leading zero if n < 1 Add trailing decimal zero(s) to fill up fixed width Truncate decimal digits past fixed width Align all decimal points For example: %…
hobbes3
  • 22,472
  • 23
  • 78
  • 114
1
2 3
99 100