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
193
votes
29 answers

Remove useless zero digits from decimals in PHP

I'm trying to find a fast way to remove zero decimals from number values like this: echo cleanNumber('125.00'); // 125 echo cleanNumber('966.70'); // 966.7 echo cleanNumber(844.011); // 844.011 Does exists some optimized way to do that?
vitto
  • 19,094
  • 31
  • 91
  • 130
191
votes
11 answers

SQL order string as number

I have numbers saved as VARCHAR to a MySQL database. I can not make them INT due to some other depending circumstances. It is taking them as character not as number while sorting. In database I have 1 2 3 4 5 6 7 8 9 10... On my page it shows…
Jamol
  • 3,768
  • 8
  • 45
  • 68
185
votes
11 answers

How can I format a String number to have commas and round?

What is the best way to format the following number that is given to me as a String? String number = "1000500000.574" //assume my value will always be a String I want this to be a String with the value: 1,000,500,000.57 How can I format it as such?
Sheehan Alam
  • 60,111
  • 124
  • 355
  • 556
178
votes
17 answers

Display numbers with ordinal suffix in PHP

I want to display numbers as follows 1 as 1st, 2 as 2nd, ..., 150 as 150th. How should I find the correct ordinal suffix (st, nd, rd or th) for each number in my code?
ArK
  • 20,698
  • 67
  • 109
  • 136
173
votes
12 answers

How do you check in python whether a string contains only numbers?

How do you check whether a string contains only numbers? I've given it a go here. I'd like to see the simplest way to accomplish this. import string def main(): isbn = input("Enter your 10 digit ISBN number: ") if len(isbn) == 10 and…
Coder77
  • 2,203
  • 5
  • 20
  • 28
171
votes
7 answers

Long vs Integer, long vs int, what to use and when?

Sometimes I see API's using long or Long or int or Integer, and I can't figure how the decision is made for that? When should I choose what?
165
votes
1 answer

How do I convert between numeric types safely and idiomatically?

Editor's note: This question is from a version of Rust prior to 1.0 and references some items that are not present in Rust 1.0. The answers still contain valuable information. What's the idiomatic way to convert from (say) a usize to a u32? For…
Caspar
  • 7,039
  • 4
  • 29
  • 41
159
votes
18 answers

How can I get a count of the total number of digits in a number?

How can I get a count of the total number of digits of a number in C#? For example, the number 887979789 has 9 digits.
Arash
  • 3,013
  • 10
  • 52
  • 74
156
votes
7 answers

Fixed point vs Floating point number

I just can't understand fixed point and floating point numbers due to hard to read definitions about them all over Google. But none that I have read provide a simple enough explanation of what they really are. Can I get a plain definition with…
user942451
  • 1,595
  • 2
  • 12
  • 6
153
votes
32 answers

How to convert number to words in java

We currently have a crude mechanism to convert numbers to words (e.g. using a few static arrays) and based on the size of the number translating that into an english text. But we are running into issues for numbers which are huge. 10183 = Ten…
Jason
  • 12,229
  • 20
  • 51
  • 66
151
votes
2 answers

Limits of Nat type in Shapeless

In shapeless, the Nat type represents a way to encode natural numbers at a type level. This is used for example for fixed size lists. You can even do calculations on type level, e.g. append a list of N elements to a list of K elements and get back a…
Rüdiger Klaehn
  • 12,445
  • 3
  • 41
  • 57
146
votes
16 answers

What is the most pythonic way to check if an object is a number?

Given an arbitrary python object, what's the best way to determine whether it is a number? Here is is defined as acts like a number in certain circumstances. For example, say you are writing a vector class. If given another vector, you want to find…
Claudiu
  • 224,032
  • 165
  • 485
  • 680
146
votes
32 answers

Generate unique random numbers between 1 and 100

How can I generate some unique random numbers between 1 and 100 using JavaScript?
dotty
  • 40,405
  • 66
  • 150
  • 195
145
votes
5 answers

How to sort numbers correctly with Array.sort()?

In multiple browsers, the following code doesn't sort the numbers correctly: a = new Array(); a.push(10); a.push(60); a.push(20); a.push(30); a.push(100); document.write(a.sort()) It returns 10,100,20,30,60. Anyone know why?
Some Guy
  • 15,854
  • 10
  • 58
  • 67
144
votes
7 answers

How to convert a negative number to positive?

How can I convert a negative number to positive in Python? (And keep a positive one.)
aneuryzm
  • 63,052
  • 100
  • 273
  • 488