Esempio n. 1
0
    def test_from_str(self):
        num = GncNumeric("3.1")
        self.assertEqual(str(num), "31/10")
        self.assertEqual(num.num(), 31)
        self.assertEqual(num.denom(), 10)

        num = GncNumeric("1/3")
        self.assertEqual(str(num), "1/3")
        self.assertEqual(num.num(), 1)
        self.assertEqual(num.denom(), 3)
Esempio n. 2
0
    def test_from_float(self):
        num = GncNumeric(3.1, 20, GNC_HOW_DENOM_FIXED | GNC_HOW_RND_NEVER)
        self.assertEqual(str(num), "62/20")
        self.assertEqual(num.num(), 62)
        self.assertEqual(num.denom(), 20)

        num = GncNumeric(1/3.0, 10000000000, GNC_HOW_RND_FLOOR)
        self.assertEqual(str(num), "3333333333/10000000000")
        self.assertEqual(num.num(), 3333333333)
        self.assertEqual(num.denom(), 10000000000)

        num = GncNumeric(1/3.0, 10000000000, GNC_HOW_RND_CEIL)
        self.assertEqual(str(num), "3333333334/10000000000")
        self.assertEqual(num.num(), 3333333334)
        self.assertEqual(num.denom(), 10000000000)
Esempio n. 3
0
    def test_from_long(self):
        num = GncNumeric(3L)
        self.assertEqual(str(num), "3/1")
        self.assertEqual(num.num(), 3)
        self.assertEqual(num.denom(), 1)

        with self.assertRaises(Exception) as context:
            GncNumeric((2**64)+1)

        #On Linux it raises an OverflowError while on MacOS it's a TypeError.
        self.assertTrue(isinstance(context.exception, TypeError) or
                        isinstance(context.exception, OverflowError))
Esempio n. 4
0
    def test_from_int(self):
        num = GncNumeric(3)
        self.assertEqual(str(num), "3/1")
        self.assertEqual(num.num(), 3)
        self.assertEqual(num.denom(), 1)

        with self.assertRaises(Exception) as context:
            GncNumeric((2**64)+1)

        #On Linux it raises an OverflowError while on MacOS it's a TypeError.
        self.assertTrue(isinstance(context.exception, TypeError) or
                        isinstance(context.exception, OverflowError))
Esempio n. 5
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))
Esempio n. 6
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))
Esempio n. 7
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) )
Esempio n. 8
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:]])
Esempio n. 9
0
 def test_defaut(self):
     num = GncNumeric()
     self.assertEqual(str(num), "0/1")
     self.assertEqual(num.num(), 0)
     self.assertEqual(num.denom(), 1)
Esempio n. 10
0
 def test_from_instance(self):
     orig = GncNumeric(3)
     num = GncNumeric(instance=orig.instance)
     self.assertEqual(str(num), "3/1")
     self.assertEqual(num.num(), 3)
     self.assertEqual(num.denom(), 1)
Esempio n. 11
0
 def test_from_float_auto(self):
     num = GncNumeric(3.1)
     self.assertEqual(str(num), "31/10")
     self.assertEqual(num.num(), 31)
     self.assertEqual(num.denom(), 10)
Esempio n. 12
0
 def test_from_int(self):
     num = GncNumeric(3)
     self.assertEqual(str(num), "3/1")
     self.assertEqual(num.num(), 3)
     self.assertEqual(num.denom(), 1)
Esempio n. 13
0
 def test_from_num_denom(self):
     num = GncNumeric(1, 2)
     self.assertEqual(str(num), "1/2")
     self.assertEqual(num.num(), 1)
     self.assertEqual(num.denom(), 2)