我有点二进制,例如0.10010011
如何将其转换为十进制浮点数?(0.10010011=0.57421875)

var a = 0.10010011
var b = point_bin2dec(a)
console.log(b) // 0.57421875

最佳答案

function point_bin2dec(num) {
    var parts = num.toString().split('.');
    return parseInt(parts[0], 2) + (parts[1] || '').split('').reduceRight(function (r, a) {
        return (r + parseInt(a, 2)) / 2;
    }, 0);
}
document.write(point_bin2dec(0.10010011));

修改此问题的答案How to convert binary fraction to decimal

09-16 18:03