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

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));

Related

Regex after first sign [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 5 years ago.
I'm gonna to write regex or other expression to get coordinates after '='.
My example:
var cords = https://maps.googleapis.com/maps/api/staticmap?center=50.082961,19.966860&zoom=13&size=300x300&sensor=false&markers=color:orange%7C50.082961,19.966860&client=gme-marktplaats&channel=bt_pl&signature=lPDQWiNQ2_mY8xgoVthZHLLYWac=
I want to get 50.082961,19.966860
I know that I could use slice but I think I could write it better with regex.
Simple base for this example: \=(.[0-9]) What's next?
Try this center\=(\d+\.\d+,\d+\.\d+)&
var val = 'https://maps.googleapis.com/maps/api/staticmap?center=50.082961,19.966860&zoom=13&size=300x300&sensor=false&markers=color:orange%7C50.082961,19.966860&client=gme-marktplaats&channel=bt_pl&signature=lPDQWiNQ2_mY8xgoVthZHLLYWac='.match(/center\=(\d+\.\d+,\d+\.\d+)&/)[1]
console.log(val)
But as other's have commented, you likely shouldn't be using regex for this purpose

How to add fraction and whole number number in javascript? [duplicate]

This question already has answers here:
How to deal with floating point number precision in JavaScript?
(47 answers)
Closed 5 years ago.
for example in javascript addition of 1 + 0.59 is 1.5899999999999999 but I want output of 1.59 which looks right to me.
Use .toFixed() to round your number.
alert((1+0.58999999999).toFixed(2));

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

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.

Is it possible to correctly do math on numbers greater than 2^53? [duplicate]

This question already has answers here:
Large integers in javascript (more the 2^53-1)
(2 answers)
What is JavaScript's highest integer value that a number can go to without losing precision?
(21 answers)
Closed 7 years ago.
I am making a calculator in JavaScript that needs to be able to do precise math on numbers larger than 2^53, which is 9007199254740992. Is there any way to do this?
You can use the "strint" library https://github.com/rauschma/strint.
For example:
> var strint = require("./strint");
> strint.add("9007199254740992", "1")
'9007199254740993'

Converting from number to string gives strange number in Javascript [duplicate]

This question already has answers here:
How do I work around JavaScript's parseInt octal behavior?
(10 answers)
Closed 8 years ago.
So I saw this strange scenario. I wanted to convert a number to a String in Node.js and I got the following.
01010100132.toString()
Turns into
"136347738"
Can someone explain this to me?
Any numeric constant prefixed with a 0 is an octal literal (assuming all its digits are valid octal digits).
var i = 010; // 8 decimal

Categories

Resources