Convert fraction Binary to Decimal | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Convert fraction Binary to Decimal

I'm trying to convert a fraction Binary Number to Decimal but I don't know how, Can you help me? I know the process, but I don't know how to put it in Javascript https://code.sololearn.com/W63RFm7xQdRU/?ref=app

31st Oct 2020, 10:32 PM
Jesús Emmanuel Justo González
Jesús Emmanuel Justo González - avatar
6 Answers
+ 3
You could use luis calderón's method with some small changes: - Count how many fractional digits there are, let's say `n`. - Remove the period from your number so it looks like a big whole binary number - .toString or parseInt it, like usual - divide by `2**n`
1st Nov 2020, 12:29 AM
Schindlabua
Schindlabua - avatar
+ 3
Just use the .toString method .toString method takes an argument, it takes a number bitweene 2 and 36, that argument means the number base, example : - binary use 2; - hexadecimal use 16; - decimal use 10; let num = document.getElementById("numeros").value; //convert to binary console.log(num.toString(2)); keep it simple ✌️
31st Oct 2020, 10:47 PM
CoffeeByte
CoffeeByte - avatar
+ 2
Para binario a decimal: let bin = document.getElementById("numero").value; //Suponiendo que el usuario ingreso 011 let decimal = Number("0b"+bin).toString (10); //decimal ahora es 3 para decimal a binario haces lo mismo, pero envés de Number("0b"+bin) Haces bin.toString(10)
31st Oct 2020, 11:01 PM
CoffeeByte
CoffeeByte - avatar
31st Oct 2020, 11:08 PM
CoffeeByte
CoffeeByte - avatar
+ 1
I did it, but it doesn't convert the number, it appears as I wrote, basically it copies the value and print it as I wrote it, I want to convert a number like "101.01" to Decimal, it's "5.25"
31st Oct 2020, 10:58 PM
Jesús Emmanuel Justo González
Jesús Emmanuel Justo González - avatar
0
Convert fraction Binary to Decimal https://code.sololearn.com/W63RFm7xQdRU/?ref=app var base=2; function conversion(){ var bin=document.getElementById("numeros").value; var decimal_1=Number.parseInt(bin,2); alert(bin+"Convertido en decimal es "+decimal_1); //console.log(bin.toString(2)); }
1st Nov 2020, 2:23 PM
Sivaprasad