How do I get only numbers in Javascript? [duplicate] - javascript

This question already has answers here:
How might I extract the number from a number + unit of measure string using JavaScript?
(6 answers)
Closed 7 years ago.
Lets say I have "13 coins". How would extract 13 from that? I tried getting the first 2 numbers but if there was 3 numbers it wouldnt work.

If that is your only example then "parseInt" should work
for example
alert(parseInt("13 coins"));

"13 coins".replace(/[^0-9.]/g,"")
This is what I would usually do (though people may accuse me of using a hammer to kill a fly!). And yes, this works with decimals.

Related

Why is js parseInt not working for this particular integer? [duplicate]

This question already has answers here:
How is the parseInt in JavaScript defined to handle large "numbers" - is there an ECMA leak? I got a wow here
(3 answers)
Why is parseInt() not converting my String of numbers correctly?
(1 answer)
Closed 4 years ago.
Here is my string: "6145390195186705543"
I have tried parseInt with different radix values. I have tried the Number() method. And I have tried multiplying it by one. They all give back 6145390195186705000
Why is this??

JS: Efficient way to convert Integer to 8 digit binary? [duplicate]

This question already has answers here:
How can I pad a value with leading zeros?
(76 answers)
Closed 8 years ago.
I know this can be accomplished in different ways, and there are already some example implementations suggested in other answers.
So what I'm asking is what is the most efficient in terms of computing way to do it.
Self explanatory:
var num = 3;
console.log(num.toString(2));
//Outputs: "11"
//Desired output: "00000011";
Thank you.
Try this
console.log(("00000000"+num.toString(2)).substr(-8));
Or as #LcSalazar stated:
console.log(("00000000"+num.toString(2)).slice(-8));

Javascript Multiplication issue with specific numbers [duplicate]

This question already has answers here:
How to deal with floating point number precision in JavaScript?
(47 answers)
Closed 8 years ago.
In javascript console type in 79.99 * 100
instead of 7999 we get 7998.999999999999
This occurs only with specific digits(80.99,80.01) etc.
Does not occur with 89.99,99.99 etc.
Is this a bug with Javascript?
Abhi.
Yes. Javascript handles numbers differently and there's a problem with javascript number handler in some cases. This can be achieved by:
function strip(number) {
return (parseFloat(number.toPrecision(12)));
}
strip(79.99*100);

Replace 56666.666666666664% percentage in to three digit percentage using javascript [duplicate]

This question already has answers here:
Formatting a number with exactly two decimals in JavaScript
(32 answers)
Closed 9 years ago.
I have calculated some data usign javascript and result is printing like this (56666.666666666664%). and I need to convert the result in like 56.66%. Below is a Javascript which print the above result.
$scope.totaTaken = (totalT / data.length)*100;
If you're looking for a JavaScript solution (where you do it in a controller or directive), the given comments answer your question (see toFixed). If you're looking for an AngularJS solution, where you format the value in the view, check out the number filter which was made for this!

How to get text equivalent of a number in JavaScript [duplicate]

This question already has answers here:
JavaScript numbers to Words
(28 answers)
Closed 8 years ago.
How to convert numeric number to their text equivalent?
Example:
Suppose if I type 3 or 23 in the text box, I want their text equivalent of three or twenty three, is this possible?
See this answer for more details:
JavaScript numbers to Words
And for a working implementation:
http://javascript.about.com/library/bltoword.htm
This will help you to achieve your task.
Number To Word

Categories

Resources