TL; DR :
var convert = (S, B) => (parseInt((S = S.toString().trim().split("."))[0].replace("-", ""), (B = +B || 2)) + (S[1] || "").split("").reduceRight((s, b) => (s + parseInt(b, B)) / B, 0)) * (+(S[0][0] !== "-") || -1);
Nina Scholz has a good example , but she does not work with negative numbers (plus other problems).
So this is an improved option:
function convert(string, base) { string = string.toString().trim().split("."); base = +base || 2; return (parseInt(string[0].replace("-", ""), base) + (string[1] || "").split("").reduceRight((sum, bit) => (sum + parseInt(bit, base)) / base, 0)) * (+(string[0][0] !== "-") || -1); } convert(1100.0011); // -12.1875 convert(-1100.0011); // -12.1875 convert("-Z.ZZZ", 36); // -35.99997856652949
As I know, JavaScript does not provide such built-in functions.
Dmitry Parzhitsky
source share