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
473
votes
24 answers

Check if a number has a decimal place/is a whole number

I am looking for an easy way in JavaScript to check if a number has a decimal place in it (in order to determine if it is an integer). For instance, 23 -> OK 5 -> OK 3.5 -> not OK 34.345 -> not OK if(number is integer) {...}
Björn
  • 12,587
  • 12
  • 51
  • 70
459
votes
18 answers

Allow 2 decimal places in

I have a and I want to restrict the input of the users to purely numbers or numbers with decimals up to 2 decimal places. Basically, I am asking for a price input. I wanted to avoid doing regex. Is there a way to do it?
nubteens
  • 5,462
  • 4
  • 20
  • 31
445
votes
18 answers

Is there any way to prevent input type="number" getting negative values?

I want to get only positive values, is there any way to prevent it using only html Please don't suggest validation method
Tural Ali
  • 22,202
  • 18
  • 80
  • 129
437
votes
31 answers

How can I set max-length in an HTML5 "input type=number" element?

For element, maxlength is not working. How can I restrict the maxlength for that number element?
Prasad
  • 58,881
  • 64
  • 151
  • 199
433
votes
22 answers

Convert boolean result into number/integer

I have a variable that stores false or true, but I need 0 or 1 instead, respectively. How can I do this?
hd.
  • 17,596
  • 46
  • 115
  • 165
354
votes
32 answers

Get decimal portion of a number with JavaScript

I have float numbers like 3.2 and 1.6. I need to separate the number into the integer and decimal part. For example, a value of 3.2 would be split into two numbers, i.e. 3 and 0.2 Getting the integer portion is easy: n = Math.floor(n); But I am…
Oscar
  • 3,551
  • 2
  • 15
  • 5
314
votes
16 answers

Why does the HTML input with type "number" allow the letter 'e' to be entered in the field?

I have the following HTML5 input element: Why does this input allow for the character 'e' to be entered in the input field? No other alphabet character is able to be entered (as expected) Using Chrome v. 44.0.2403.107 Example…
Gnarlywhale
  • 4,030
  • 2
  • 15
  • 18
313
votes
36 answers

Format a number as 2.5K if a thousand or more, otherwise 900

I need to show a currency value in the format of 1K of equal to one thousand, or 1.1K, 1.2K, 1.9K etc, if its not an even thousands, otherwise if under a thousand, display normal 500, 100, 250 etc, using JavaScript to format the number?
Carl Weis
  • 6,794
  • 15
  • 63
  • 86
303
votes
17 answers

Test if number is odd or even

What is the simplest most basic way to find out if a number/variable is odd or even in PHP? Is it something to do with mod? I've tried a few scripts but.. google isn't delivering at the moment.
user1022585
  • 13,061
  • 21
  • 55
  • 75
286
votes
1 answer

python re.sub group: number after \number

How can I replace foobar with foo123bar? This doesn't work: >>> re.sub(r'(foo)', r'\1123', 'foobar') 'J3bar' This works: >>> re.sub(r'(foo)', r'\1hi', 'foobar') 'foohibar' I think it's a common issue when having something like \number. Can anyone…
zhigang
  • 6,597
  • 4
  • 30
  • 24
276
votes
5 answers

How can I check if my python object is a number?

In Java the numeric types all descend from Number so I would use (x instanceof Number). What is the python equivalent?
Neal Ehardt
  • 10,334
  • 9
  • 41
  • 51
263
votes
24 answers

Remove insignificant trailing zeros from a number?

Have I missed a standard API call that removes trailing insignificant zeros from a number? var x = 1.234000; // to become 1.234 var y = 1.234001; // stays 1.234001 Number.toFixed() and Number.toPrecision() are not quite what I'm looking for.
Steven
  • 2,789
  • 2
  • 18
  • 12
260
votes
24 answers

Add st, nd, rd and th (ordinal) suffix to a number

I would like to dynamically generate a string of text based on a current day. So, for example, if it is day 1 then I would like my code to generate = "Its the 1*st*". There are 12 days in total so…
Antonio Vasilev
  • 2,845
  • 2
  • 14
  • 17
258
votes
17 answers

Check whether an input string contains a number in javascript

My end goal is to validate an input field. The input may be either alphabetic or numeric.
Udara S.S Liyanage
  • 6,189
  • 9
  • 33
  • 34
257
votes
35 answers

How to elegantly check if a number is within a range?

How can I do this elegantly with C#? For example, a number can be between 1 and 100. I know a simple if (x >= 1 && x <= 100) would suffice; but with a lot of syntax sugar and new features constantly added to C#/.Net this question is about more…
Sergio Tapia
  • 40,006
  • 76
  • 183
  • 254