GnuCash 7574033+
Loading...
Searching...
No Matches
python.gnucash_core.GncNumeric Class Reference
Inheritance diagram for python.gnucash_core.GncNumeric:
python.gnucash_core.GnuCashCoreClass

Public Member Functions

 __init__ (self, *args, **kargs)
 __setattr__ (self, name, value)
 __lt__ (a, b)
 __gt__ (a, b)
 __le__ (a, b)
 __ge__ (a, b)
 __eq__ (a, b)
 __bool__ (a)
 __float__ (self)
 __int__ (self)
 __pos__ (a)
 __neg__ (a)
 __abs__ (a)
 to_fraction (self)
 __str__ (self)
Public Member Functions inherited from python.gnucash_core.GnuCashCoreClass
 do_lookup_create_oo_instance (self, lookup_function, cls, *args)

Data Fields

 instance

Protected Member Functions

 _operator_fallbacks (monomorphic_operator, fallback_operator)
 _add (a, b)
 _sub (a, b)
 _mul (a, b)
 _div (a, b)
 _floordiv (a, b)
 _lt (a, b)
 _gt (a, b)
 _le (a, b)
 _ge (a, b)
 _eq (a, b)
 _richcmp (self, other, op)

Additional Inherited Members

Static Protected Attributes inherited from python.gnucash_core.GnuCashCoreClass
 _module = gnucash_core_c

Detailed Description

Object used by GnuCash to store all numbers. Always consists of a
numerator and denominator.

The constants GNC_DENOM_AUTO,
GNC_HOW_RND_FLOOR, GNC_HOW_RND_CEIL, GNC_HOW_RND_TRUNC,
GNC_HOW_RND_PROMOTE, GNC_HOW_RND_ROUND_HALF_DOWN,
GNC_HOW_RND_ROUND_HALF_UP, GNC_HOW_RND_ROUND, GNC_HOW_RND_NEVER,
GNC_HOW_DENOM_EXACT, GNC_HOW_DENOM_REDUCE, GNC_HOW_DENOM_LCD,
and GNC_HOW_DENOM_FIXED are available for arithmetic
functions like GncNumeric.add

Look at gnc-numeric.h to see how to use these

Definition at line 537 of file gnucash_core.py.

Constructor & Destructor Documentation

◆ __init__()

python.gnucash_core.GncNumeric.__init__ ( self,
* args,
** kargs )
Constructor that supports the following formats:
* No arguments defaulting to zero: eg. GncNumeric() == 0/1
* A integer: e.g. GncNumeric(1) == 1/1
* Numerator and denominator intager pair: eg. GncNumeric(1, 2) == 1/2
* A floating point number: e.g. GncNumeric(0.5) == 1/2
* A floating point number with defined conversion: e.g.
  GncNumeric(0.5, GNC_DENOM_AUTO,
            GNC_HOW_DENOM_FIXED | GNC_HOW_RND_NEVER) == 1/2
* A string: e.g. GncNumeric("1/2") == 1/2

Definition at line 552 of file gnucash_core.py.

552 def __init__(self, *args, **kargs):
553 """Constructor that supports the following formats:
554 * No arguments defaulting to zero: eg. GncNumeric() == 0/1
555 * A integer: e.g. GncNumeric(1) == 1/1
556 * Numerator and denominator intager pair: eg. GncNumeric(1, 2) == 1/2
557 * A floating point number: e.g. GncNumeric(0.5) == 1/2
558 * A floating point number with defined conversion: e.g.
559 GncNumeric(0.5, GNC_DENOM_AUTO,
560 GNC_HOW_DENOM_FIXED | GNC_HOW_RND_NEVER) == 1/2
561 * A string: e.g. GncNumeric("1/2") == 1/2
562 """
563 if 'instance' not in kargs:
564 kargs['instance'] = GncNumeric.__args_to_instance(args)
565 GnuCashCoreClass.__init__(self, [], **kargs)
566

Member Function Documentation

◆ __abs__()

python.gnucash_core.GncNumeric.__abs__ ( a)
abs(a)

Definition at line 735 of file gnucash_core.py.

735 def __abs__(a):
736 """abs(a)"""
737 return a.abs()
738

◆ __bool__()

python.gnucash_core.GncNumeric.__bool__ ( a)
a != 0

Definition at line 717 of file gnucash_core.py.

717 def __bool__(a):
718 """a != 0"""
719 return bool(a.num())
720

◆ __eq__()

python.gnucash_core.GncNumeric.__eq__ ( a,
b )
a == b

Definition at line 713 of file gnucash_core.py.

713 def __eq__(a, b):
714 """a == b"""
715 return a._richcmp(b, a._eq)
716

◆ __float__()

python.gnucash_core.GncNumeric.__float__ ( self)

Definition at line 721 of file gnucash_core.py.

721 def __float__(self):
722 return self.to_double()
723

◆ __ge__()

python.gnucash_core.GncNumeric.__ge__ ( a,
b )
a >= b

Definition at line 709 of file gnucash_core.py.

709 def __ge__(a, b):
710 """a >= b"""
711 return a._richcmp(b, a._ge)
712

◆ __gt__()

python.gnucash_core.GncNumeric.__gt__ ( a,
b )
a > b

Definition at line 701 of file gnucash_core.py.

701 def __gt__(a, b):
702 """a > b"""
703 return a._richcmp(b, a._gt)
704

◆ __int__()

python.gnucash_core.GncNumeric.__int__ ( self)

Definition at line 724 of file gnucash_core.py.

724 def __int__(self):
725 return int(self.to_fraction())
726

◆ __le__()

python.gnucash_core.GncNumeric.__le__ ( a,
b )
a <= b

Definition at line 705 of file gnucash_core.py.

705 def __le__(a, b):
706 """a <= b"""
707 return a._richcmp(b, a._le)
708

◆ __lt__()

python.gnucash_core.GncNumeric.__lt__ ( a,
b )
a < b

Definition at line 697 of file gnucash_core.py.

697 def __lt__(a, b):
698 """a < b"""
699 return a._richcmp(b, a._lt)
700

◆ __neg__()

python.gnucash_core.GncNumeric.__neg__ ( a)
-a

Definition at line 731 of file gnucash_core.py.

731 def __neg__(a):
732 """-a"""
733 return a.neg()
734

◆ __pos__()

python.gnucash_core.GncNumeric.__pos__ ( a)
+a

Definition at line 727 of file gnucash_core.py.

727 def __pos__(a):
728 """+a"""
729 return GncNumeric(a.num(), a.denom())
730
The primary numeric class for representing amounts and values.

◆ __setattr__()

python.gnucash_core.GncNumeric.__setattr__ ( self,
name,
value )

Definition at line 567 of file gnucash_core.py.

567 def __setattr__(self, name, value):
568 # num and denom are read-only accessor methods, not writable fields.
569 if name in ('num', 'denom'):
570 raise AttributeError(
571 "Cannot assign to '%s': it is a read-only accessor method on "
572 "GncNumeric, not a writable field, so the assignment would not "
573 "change the value. Construct a new value instead, e.g. "
574 "GncNumeric(numerator, denominator)." % name)
575 super().__setattr__(name, value)
576

◆ __str__()

python.gnucash_core.GncNumeric.__str__ ( self)
Returns a human readable numeric value string as UTF8.

Definition at line 746 of file gnucash_core.py.

746 def __str__(self):
747 """Returns a human readable numeric value string as UTF8."""
748 return gnc_numeric_to_string(self.instance)
749
gchar * gnc_numeric_to_string(gnc_numeric n)
Convert to string.

◆ _add()

python.gnucash_core.GncNumeric._add ( a,
b )
protected

Definition at line 638 of file gnucash_core.py.

638 def _add(a, b):
639 return a.add(b, GNC_DENOM_AUTO, GNC_HOW_RND_ROUND)
640

◆ _div()

python.gnucash_core.GncNumeric._div ( a,
b )
protected

Definition at line 647 of file gnucash_core.py.

647 def _div(a, b):
648 return a.div(b, GNC_DENOM_AUTO, GNC_HOW_RND_ROUND)
649

◆ _eq()

python.gnucash_core.GncNumeric._eq ( a,
b )
protected

Definition at line 677 of file gnucash_core.py.

677 def _eq(a, b):
678 return a.compare(b) == 0
679

◆ _floordiv()

python.gnucash_core.GncNumeric._floordiv ( a,
b )
protected

Definition at line 650 of file gnucash_core.py.

650 def _floordiv(a, b):
651 return a.div(b, 1, GNC_HOW_RND_FLOOR)
652

◆ _ge()

python.gnucash_core.GncNumeric._ge ( a,
b )
protected

Definition at line 674 of file gnucash_core.py.

674 def _ge(a, b):
675 return a.compare(b) in (0,1)
676

◆ _gt()

python.gnucash_core.GncNumeric._gt ( a,
b )
protected

Definition at line 668 of file gnucash_core.py.

668 def _gt(a, b):
669 return a.compare(b) == 1
670

◆ _le()

python.gnucash_core.GncNumeric._le ( a,
b )
protected

Definition at line 671 of file gnucash_core.py.

671 def _le(a, b):
672 return a.compare(b) in (0,-1)
673

◆ _lt()

python.gnucash_core.GncNumeric._lt ( a,
b )
protected

Definition at line 665 of file gnucash_core.py.

665 def _lt(a, b):
666 return a.compare(b) == -1
667

◆ _mul()

python.gnucash_core.GncNumeric._mul ( a,
b )
protected

Definition at line 644 of file gnucash_core.py.

644 def _mul(a, b):
645 return a.mul(b, GNC_DENOM_AUTO, GNC_HOW_RND_ROUND)
646

◆ _operator_fallbacks()

python.gnucash_core.GncNumeric._operator_fallbacks ( monomorphic_operator,
fallback_operator )
protected
fallbacks are not needed except for method name,
keep for possible later use

Definition at line 614 of file gnucash_core.py.

614 def _operator_fallbacks(monomorphic_operator, fallback_operator):
615 """fallbacks are not needed except for method name,
616 keep for possible later use"""
617 def forward(a, b):
618 if isinstance(b, GncNumeric):
619 return monomorphic_operator(a, b)
620 if isinstance(b, (int, float)):
621 return monomorphic_operator(a, GncNumeric(b))
622 else:
623 return NotImplemented
624 forward.__name__ = '__' + fallback_operator.__name__ + '__'
625 forward.__doc__ = monomorphic_operator.__doc__
626
627 def reverse(b, a):
628 # b is self, the right-hand operand; a is the left-hand one.
629 if isinstance(a, (GncNumeric, int, float)):
630 return forward(GncNumeric(a), b)
631 else:
632 return NotImplemented
633 reverse.__name__ = '__r' + fallback_operator.__name__ + '__'
634 reverse.__doc__ = monomorphic_operator.__doc__
635
636 return forward, reverse
637

◆ _richcmp()

python.gnucash_core.GncNumeric._richcmp ( self,
other,
op )
protected
Helper for comparison operators, for internal use only.
Implement comparison between a GncNumeric instance `self`,
and either another GncNumeric instance, an int or a float
`other`.  If `other` is not an instance of that kind, return
NotImplemented. `op` should be one of the six standard
comparison operators. The comparisons are based on
GncNumeric.compare().

Definition at line 680 of file gnucash_core.py.

680 def _richcmp(self, other, op):
681 """Helper for comparison operators, for internal use only.
682 Implement comparison between a GncNumeric instance `self`,
683 and either another GncNumeric instance, an int or a float
684 `other`. If `other` is not an instance of that kind, return
685 NotImplemented. `op` should be one of the six standard
686 comparison operators. The comparisons are based on
687 GncNumeric.compare().
688 """
689 import math
690 if isinstance(other, GncNumeric):
691 return op(other)
692 elif isinstance(other, (int, float)):
693 return op(GncNumeric(other))
694 else:
695 return NotImplemented
696

◆ _sub()

python.gnucash_core.GncNumeric._sub ( a,
b )
protected

Definition at line 641 of file gnucash_core.py.

641 def _sub(a, b):
642 return a.sub(b, GNC_DENOM_AUTO, GNC_HOW_RND_ROUND)
643

◆ to_fraction()

python.gnucash_core.GncNumeric.to_fraction ( self)

Definition at line 739 of file gnucash_core.py.

739 def to_fraction(self):
740 from fractions import Fraction
741 # A negative denominator multiplies: 3/-100 is 300.
742 if self.denom() < 0:
743 return Fraction(self.num() * -self.denom())
744 return Fraction(self.num(), self.denom())
745

Field Documentation

◆ instance

python.gnucash_core.GncNumeric.instance

Definition at line 748 of file gnucash_core.py.


The documentation for this class was generated from the following file: