@numio/bigmath
Version:
@numio/bigmath is an arbitrary-precision arithmetic library. It can be used for basic operations with decimal numbers (integers and float)
15 lines (14 loc) • 415 B
JavaScript
import { s2bi } from "../shared/utils.js";
/** Convert number to another base */
export const toBase = ({ value, toBase }) => {
const [bi] = s2bi(value);
const isNegative = value[0] === "-";
const map = {
2: "0b",
8: "0o",
10: "",
16: "0x",
};
return (isNegative ? "-" : "") + map[toBase] +
(isNegative ? -1n * bi : bi).toString(toBase).toUpperCase();
};