-2

I am trying to convert decimal to binary numbers using AngularJS

let decimal = 128; let binary = parseInt(decimal.toString(), 2); // returning 1 
4
  • 1
    try function dec2bin(dec){ return (dec >>> 0).toString(2); } Commented Jun 29, 2017 at 7:43
  • 1
    Possible duplicate of String Conversion in Javascript (Decimal to Binary) Commented Jun 29, 2017 at 7:44
  • 1
    @RahalKanishka, Thanks working. Commented Jun 29, 2017 at 7:47
  • There is no decimal here. decimal is already in binary. Commented Aug 22, 2017 at 4:22

3 Answers 3

0

You need to use toString() and not parseInt. This line of code work

let decimal = 128; var toBinary = (+decimal).toString(2); console.log(toBinary); 
Sign up to request clarification or add additional context in comments.

1 Comment

You can also upvote the answer so that it can depict that the answer has been really useful to you.
0

The below code will convert numbers to the base 2 positional numeral system return (decimal >>> 0).toString(2)

Comments

0

try this simple way

(128).toString(2); //result is 10000000 

Snippet

<!DOCTYPE html> <html> <body> <p>Convert binary number from 128.</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunction() { document.getElementById("demo").innerHTML = (128).toString(2); } </script> </body> </html>

3 Comments

didn't you see the comment in the question the OP has already got the solution for this then why are you adding the same thing again in different way?
I just posted single line of code instead of other answers. Answers is should be better to solve the problem
What about all that HTML and the button and click. They are irrelevant in this case. Why did not you added console.log() simple as that, if you are really worried about simplicity?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.