numerica
    Preparing search index...

    Function toBase

    • Converts a number from one base to another base (supports fractional and negative numbers).

      Parameters

      • n: string | number

        The number to convert. Can be a numeric value or string

      • to: number

        The target base to convert to (e.g., 2 for binary, 16 for hexadecimal)

      • Optionaloptions: Options = {}

        Configuration options

        • from

          The original base of the input number. Defaults to base 10

        • precision

          The maximum number of digits to include after the decimal point when converting fractional values

      Returns string

      The number represented in the target base

      Throws an error if the base exceeds 62 or if the number contains invalid digits

      // Integer conversions
      toBase(255, 16) // "FF" (decimal to hexadecimal)
      toBase(255, 2) // "11111111" (decimal to binary)
      toBase('FF', 10, { from: 16 }) // "255" (hexadecimal to decimal)
      toBase('1010', 10, { from: 2 }) // "10" (binary to decimal)
      toBase(100, 36) // "2S" (decimal to base-36)
      toBase(-42, 16) // "-2A" (negative numbers supported)

      // Fractional conversions
      toBase(10.5, 16) // "A.8" (decimal to hexadecimal with fractional part)
      toBase(10.75, 2) // "1010.11" (decimal to binary with fractional part)
      toBase('A.8', 10, { from: 16 }) // "10.5" (hexadecimal with fraction to decimal)
      toBase('-2A.4', 10, { from: 16 }) // "-42.25" (negative fractional conversion)