# 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].

**1135**

votes

**42**answers

726k views

### 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 ...

**2129**

votes

**48**answers

1.4m views

### 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') ...

**485**

votes

**15**answers

272k views

### 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();...

**387**

votes

**20**answers

719k views

### Show a number to 2 decimal places

What's the correct way to round a PHP string to 2 decimal places?$number="520"; // It's a string from a DB$formatted_number=round_to_2dp($number);echo $formatted_number;The output should ...

**462**

votes

**11**answers

376k views

### 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?

**263**

votes

**11**answers

482k views

### Python: Extract numbers from a string

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]

**600**

votes

**39**answers

537k views

### 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

**38**

votes

**8**answers

29k views

### Integer with leading zeroes

When I write System.out.println(0123); I get 83 however System.out.println((int)0123F); prints 123.Why does it work that way?

**97**

votes

**26**answers

221k views

### 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 ...

**8**

votes

**10**answers

34k views

### Java arrays printing out weird numbers, and text [duplicate]

I'm new to programming. I'm sure the answer for this question is out there, but I have no idea what to search for. so um. ok, i'll go right to it.Here's my code: int[] arr;arr=new int[...

**82**

votes

**7**answers

20k views

### 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 ...

**71**

votes

**7**answers

163k views

### Large Numbers in Java

How would I go about doing calculations with extremely large numbers in Java? I have tried long but that maxes out at 9223372036854775807, and when using an integer it does not save enough digits ...

**8**

votes

**2**answers

14k views

### Python math is wrong [duplicate]

Possible Duplicate:Python rounding error with float numbers Python 2.7.3 (v2.7.3:70274d53c1dd, Apr 9 2012, 20:52:43) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwinType "copyright", "...

**49**

votes

**10**answers

59k views

### Matching numbers with regular expressions — only digits and commas

I can't figure out how to construct a regex for the example values:123,456,789-12,341234-8Could you help me?

**61**

votes

**23**answers

119k views

### Generate unique random numbers between 1 and 100

How can I generate 8, say, unique random numbers between 1 and 100 using JavaScript?

**693**

votes

**13**answers

393k views

### 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 ...

**33**

votes

**6**answers

163k views

### Restricting JTextField input to Integers [duplicate]

I know that this question must have been asked and answered a million times, but I just can't find an easy solution. I have a JTextField that is meant to accept only positive integers as input. I need ...

**48**

votes

**15**answers

79k views

### How to find the cumulative sum of numbers in a list?

time_interval=[4, 6, 12]I want to sum up the numbers like [4, 4+6, 4+6+12] in order to get the list t=[4, 10, 22].I tried the following:for i in time_interval:t1=time_interval[0]...

**264**

votes

**21**answers

433k views

### How can I limit possible inputs in a HTML5 “number” element?

For <input type="number"> element, maxlength is not working. How can I restrict the maxlength for that number element?

**50**

votes

**14**answers

55k views

### 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 ...

**70**

votes

**17**answers

350k views

### Format numbers in JavaScript similar to C#

Is there a simple way to format numbers in JavaScript, similar to the formatting methods available in C# (or VB.NET) via ToString("format_provider") or String.Format()?

**77**

votes

**13**answers

102k views

### How to compare two floating point numbers in Bash?

I am trying hard to compare two floating point numbers within a bash script. I have to variables, e.g.let num1=3.17648e-22let num2=1.5Now, I just want do a simple comparison of these two numbers:...

**24**

votes

**7**answers

32k views

### How to mathematically evaluate a string like “2-1” to produce “1”?

I was just wondering if PHP has a function that can take a string like 2-1 and produce the arithmetic result of it?Or will I have to do this manually with explode() to get the values left and right ...

**41**

votes

**12**answers

42k views

### Is there a way to convert number words to Integers?

I need to convert one into 1, two into 2 and so on.Is there a way to do this with a library or a class or anything?

**234**

votes

**15**answers

297k views

### JavaScript get number from string

I have a string in javascript like `#box2' and I just want the '2' from it.Tried:var thestring=$(this).attr('href');var thenum=thestring.replace( /(^.+)(\w\d+\w)(.+$)/i,'$2');alert(thenum);...

**28**

votes

**19**answers

48k views

### JavaScript numbers to Words

I'm trying to convert numbers into english words, for example 1234 would become: "one thousand two hundred thirty four".My Tactic goes like this:Separate the digits to three and put them on Array (...

**94**

votes

**26**answers

44k views

### How to convert floats to human-readable fractions?

Let's say we have 0.33, we need to output "1/3". If we have "0.4", we need to output "2/5".The idea is to make it human-readable to make the user understand "x parts out of y" as a better way of ...

**37**

votes

**6**answers

28k views

### Why is 08 not a valid integer literal in Java?

Why is 08 considered an out of range int but 07 and below are not?

**188**

votes

**10**answers

295k views

### Format Float to n decimal places

I need to format a float to "n"decimal places.was trying to BigDecimal, but the return value is not correct...public static float Redondear(float pNumero, int pCantidadDecimales) {// the ...

**130**

votes

**10**answers

166k views

### Phone: numeric keyboard for text input

Is there a way to force the number keyboard to come up on the phone for an <input type="text">? I just realized that <input type="number"> in HTML5 is for “floating-point numbers”, so it ...