Пример #1
0
def format_text(value: float,
                dimrnd: float = None,
                dimdec: int = None,
                dimzin: int = 0,
                dimdsep: str = '.',
                dimpost: str = '<>') -> str:
    if dimrnd is not None:
        value = xround(value, dimrnd)

    if dimdec is None:
        fmt = "{:f}"
        dimzin = dimzin | 8  # remove pending zeros for undefined decimal places, '{:f}'.format(0) -> '0.000000'
    else:
        fmt = "{:." + str(dimdec) + "f}"
    text = fmt.format(value)

    leading = bool(dimzin & 4)
    pending = bool(dimzin & 8)
    text = suppress_zeros(text, leading, pending)
    if dimdsep != '.':
        text = text.replace('.', dimdsep)
    if dimpost:
        if '<>' in dimpost:
            fmt = dimpost.replace('<>', '{}', 1)
            text = fmt.format(text)
        else:
            raise DXFValueError('Invalid dimpost string: "{}"'.format(dimpost))
    return text
Пример #2
0
def test_leading_zeros():
    assert suppress_zeros('0') == '0'
    assert suppress_zeros('0.', leading=True, trailing=True) == '0'
    assert suppress_zeros('0.0', leading=True, trailing=True) == '0'
    assert suppress_zeros('.0', leading=True, trailing=True) == '0'
    assert suppress_zeros('-.0', leading=True, trailing=True) == '0'
    assert suppress_zeros('+.0', leading=True, trailing=True) == '0'

    assert suppress_zeros('0.0', leading=False, trailing=True) == '0'
    assert suppress_zeros('0.1', leading=False, trailing=True) == '0.1'
    assert suppress_zeros('1.0', leading=False, trailing=True) == '1'
    assert suppress_zeros('1.0000', leading=False, trailing=True) == '1'
    assert suppress_zeros('-1.', leading=False, trailing=True) == '-1'

    assert suppress_zeros('0.10', leading=True, trailing=False) == '.10'
    assert suppress_zeros('-0.10', leading=True, trailing=False) == '-.10'
    assert suppress_zeros('+000.10', leading=True, trailing=False) == '+.10'
Пример #3
0
def test_leading_zeros():
    assert suppress_zeros("0") == "0"
    assert suppress_zeros("0.", leading=True, trailing=True) == "0"
    assert suppress_zeros("0.0", leading=True, trailing=True) == "0"
    assert suppress_zeros(".0", leading=True, trailing=True) == "0"
    assert suppress_zeros("-.0", leading=True, trailing=True) == "0"
    assert suppress_zeros("+.0", leading=True, trailing=True) == "0"

    assert suppress_zeros("0.0", leading=False, trailing=True) == "0"
    assert suppress_zeros("0.1", leading=False, trailing=True) == "0.1"
    assert suppress_zeros("1.0", leading=False, trailing=True) == "1"
    assert suppress_zeros("1.0000", leading=False, trailing=True) == "1"
    assert suppress_zeros("-1.", leading=False, trailing=True) == "-1"

    assert suppress_zeros("0.10", leading=True, trailing=False) == ".10"
    assert suppress_zeros("-0.10", leading=True, trailing=False) == "-.10"
    assert suppress_zeros("+000.10", leading=True, trailing=False) == "+.10"
Пример #4
0
def test_trim_trailing_zeros_for_integers():
    assert suppress_zeros("0", trailing=True) == "0"
    assert suppress_zeros("1", trailing=True) == "1"
    assert suppress_zeros("-1", trailing=True) == "-1"
    assert suppress_zeros("10", trailing=True) == "10"
    assert suppress_zeros("-10", trailing=True) == "-10"