Function parseUnits

  • Function to format a decimal point number in an integer, without rounding.

    The value must be a string made of ASCII digits, an optional leading "-" and at most one "." or "," decimal separator, with at least one digit. Surrounding whitespace is ignored. Fractional digits beyond decimals are accepted only when they are zeros (the value is still exact); any other excess digit throws, so that the amount shown to the user is always the amount that gets encoded.

    Throws

    Error if the value is not a string in that format, cannot be represented exactly with decimals decimals, or decimals is not a non-negative integer.

    Example

    const amount = parseUnits("0.00123456", 8);
    console.log(amount);
    // '123456'

    parseUnits("1.000000000", 8); // '100000000' (exact)
    parseUnits("1.999999995", 8); // throws: not representable

    Parameters

    • value: string
    • decimals: number

    Returns string

Generated using TypeDoc