+ 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
6 Respuestas
+ 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`
+ 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 ✌️
+ 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)
+ 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"
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));
}