コード例 #1
0
 def from_user(self, x):
     factor = self.factor
     if factor is None:
         return x
     x = x.remainder_near(factor)
     pi_4 = Decimal("1.0").atan()
     return x * pi_4 * 8 / factor
コード例 #2
0
def moneyfmt(value,
             places=2,
             curr="",
             sep=",",
             dp=".",
             pos="",
             neg="-",
             trailneg=""):
    """Convert Decimal to a money formatted string.

    places:  required number of places after the decimal point
    curr:    optional currency symbol before the sign (may be blank)
    sep:     optional grouping separator (comma, period, space, or blank)
    dp:      decimal point indicator (comma or period)
             only specify as blank when places is zero
    pos:     optional sign for positive numbers: '+', space or blank
    neg:     optional sign for negative numbers: '-', '(', space or blank
    trailneg:optional trailing minus indicator:  '-', ')', space or blank

    >>> d = Decimal('-1234567.8901')
    >>> moneyfmt(d, curr='$')
    '-$1,234,567.89'
    >>> moneyfmt(d, places=0, sep='.', dp='', neg='', trailneg='-')
    '1.234.568-'
    >>> moneyfmt(d, curr='$', neg='(', trailneg=')')
    '($1,234,567.89)'
    >>> moneyfmt(Decimal(123456789), sep=' ')
    '123 456 789.00'
    >>> moneyfmt(Decimal('-0.02'), neg='<', trailneg='>')
    '<0.02>'

    """
    q = Decimal(10)**-places  # 2 places --> '0.01'
    sign, digits, _ = value.quantize(q).as_tuple()
    result = []
    digits = list(map(str, digits))
    build, next = result.append, digits.pop
    if sign:
        build(trailneg)
    for i in range(places):
        build(next() if digits else "0")
    if places:
        build(dp)
    if not digits:
        build("0")
    i = 0
    while digits:
        build(next())
        i += 1
        if i == 3 and digits:
            i = 0
            build(sep)
    build(curr)
    build(neg if sign else pos)
    return "".join(reversed(result))
コード例 #3
0
 def to_user(self, x):
     factor = self.factor
     if factor is None:
         return x
     pi_4 = Decimal("1.0").atan()
     return x * factor / pi_4 / 8
コード例 #4
0
def loop():
    impl.start_redraw()
    pstack(f'{gc.mem_free()} RPN bytes free')
    impl.setline(5, "> " + "".join(entry) + "_")
    impl.refresh()
    impl.end_redraw()

    while True:
        do_pstack = False
        do_pentry = False
        message = ''

        c = impl.getch()
        if c in '\x7f\x08':
            if entry:
                entry.pop()
                do_pentry = True
            elif stack:
                stack.pop()
                do_pstack = True
        if c == '\x1b':
            del entry[:]
            do_pentry = True
        elif c in '0123456789.eE':
            if c == '.' and '.' in entry:
                c = 'e'
            entry.append(c)
            do_pentry = True
        elif c == '\x04':
            break
        elif c in ' \n':
            if entry:
                try:
                    stack.append(Decimal("".join(entry)))
                except Exception as e:
                    message = str(e)
                del entry[:]
            elif c == '\n' and stack:
                stack.append(stack[-1])
            do_pstack = True
        elif c in ops:
            if entry:
                try:
                    stack.append(Decimal("".join(entry)))
                except Exception as e:
                    message = str(e)
                del entry[:]
            op = ops.get(c)
            try:
                if callable(op):
                    message = op() or ''
                else:
                    message = do_op(*op) or ''
            except (KeyboardInterrupt, SystemExit):
                raise
            except Exception as e:
                message = str(e)
            do_pstack = True

        impl.start_redraw()

        if do_pstack:
            pstack(message)
            do_pentry = True

        if do_pentry:
            impl.setline(5, "> " + "".join(entry) + "_")

        if do_pentry or do_pstack:
            impl.refresh()

        impl.end_redraw()
コード例 #5
0
    result = []
    digits = list(map(str, digits))
    build, next = result.append, digits.pop
    if sign:
        build(trailneg)
    for i in range(places):
        build(next() if digits else "0")
    if places:
        build(dp)
    if not digits:
        build("0")
    i = 0
    while digits:
        build(next())
        i += 1
        if i == 3 and digits:
            i = 0
            build(sep)
    build(curr)
    build(neg if sign else pos)
    return "".join(reversed(result))


if __name__ == "__main__":
    d = Decimal("-1234567.8901")
    print(moneyfmt(d, curr="$"))
    print(moneyfmt(d, places=0, sep=".", dp="", neg="", trailneg="-"))
    print(moneyfmt(d, curr="$", neg="(", trailneg=")"))
    print(moneyfmt(Decimal(123456789), sep=" "))
    print(moneyfmt(Decimal("-0.02"), neg="<", trailneg=">"))