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 it to work? It needs to accommodate for any length number attached on the end. 27 … Read more

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+”” 24 s 24 like this: var foo = 45; var bar=”” + foo; Actually, even though I typically do it like this for simple convenience, over 1,000s … Read more

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() function definition be? 25 s 25 You can use number_format(): return number_format((float)$number, 2, ‘.’, ”); Example: $foo = … Read more

Formatting a number with leading zeros in PHP [duplicate]

This question already has answers here: Zero-pad digits in string (5 answers) Closed 1 year ago. 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? 1Best Answer 11 Use sprintf : sprintf(‘%08d’, 1234567); Alternatively you can also … Read more

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. <input id=”test” type=”number”> 20 20 This CSS effectively hides the spin-button for webkit … Read more