コード例 #1
0
def gnc_numeric_to_python_decimal(numeric):
    negative = numeric.negative_p()
    sign = 1 if negative else 0

    val = GncNumeric(numeric.num(), numeric.denom())
    result = val.to_decimal(None)
    if not result:
        raise Exception("gnc numeric value {} CAN'T be converted to decimal!".format(val.to_string()))

    digit_tuple = tuple(int(char) for char in str(val.num()) if char != '-')
    denominator = val.denom()
    exponent = int(log10(denominator))
    assert( (10 ** exponent) == denominator )
    return Decimal((sign, digit_tuple, -exponent))
コード例 #2
0
def gnc_numeric_to_python_Decimal(numeric):
    negative = numeric.negative_p()
    if negative:
        sign = 1
    else:
        sign = 0
    copy = GncNumeric(numeric.num(), numeric.denom())
    result = copy.to_decimal(None)
    if not result:
        raise Exception("gnc numeric value %s can't be converted to decimal" %
                        copy.to_string())
    digit_tuple = tuple(int(char) for char in str(copy.num()) if char != '-')
    denominator = copy.denom()
    exponent = int(log10(denominator))
    assert ((10**exponent) == denominator)
    return Decimal((sign, digit_tuple, -exponent))
コード例 #3
0
def gnc_numeric_to_python_Decimal(numeric):
    negative = numeric.negative_p()
    if negative:
        sign = 1
    else:
        sign = 0
    copy = GncNumeric(numeric.num(), numeric.denom())
    result = copy.to_decimal(None)
    if not result:
        raise Exception("gnc numeric value %s can't be converted to deciaml" %
                        copy.to_string() )
    digit_tuple = tuple( int(char)
                         for char in str(copy.num())
                         if char != '-' )
    denominator = copy.denom()
    exponent = int(log10(denominator))
    assert( (10 ** exponent) == denominator )
    return Decimal( (sign, digit_tuple, -exponent) )
コード例 #4
0
def to_string_with_decimal_point_placed(number: GncNumeric):
    """Convert a GncNumeric to a string with decimal point placed if permissible.
    Otherwise returns its fractional representation.
    """

    number = copy.copy(number)
    if not number.to_decimal(None):
        return str(number)

    nominator = str(number.num())
    point_place = str(number.denom()).count('0')  # How many zeros in the denominator?
    if point_place == 0:
        return nominator

    if len(nominator) <= point_place:  # prepending zeros if the nominator is too short
        nominator = '0' * (point_place - len(nominator)) + nominator

    return '.'.join([nominator[:-point_place - 1], nominator[-point_place:]])