import re


def _scientific_notation(value, sci_lims, fmt):
    """Converts value to scientific notation

    Parameters
    ----------
    value : float
        Value to process

    sci_lims : (int, int)
        See description in :py:func:`write_result`

    fmt : 'txt', 'pdf', or 'csv'
        File format.
    """
    # If user speicifed to never user scientific notation, or the value
    # does not have a sufficiently high exponent, or the file type is a csv,
    # simply return the value unedited, as a string.
    if (sci_lims is None or
            (abs(value) < 10 ** sci_lims[1] and
             abs(value) >= 10 ** (sci_lims[0])) or
            fmt == 'csv'):
        return str(value)

    # Convert to scientific notation
    # Regex is used to remove any trailing zeros, ie:
    # 12345000e8 -> 12345e8
    value = re.sub(
        r'0+e', 'e', f'{value:e}'.replace('+0', '').replace('-0', '-'),
    )

    if fmt == 'txt':
        return value
    # For LaTeX, convert to \num{value}
    # This is a siunitx macro which takes a value of the form
    # 1234e5 and converts to 1234x10⁵
    elif fmt == 'pdf':
        return f'\\num{{{value}}}'