GnuCash c935c2f+
Loading...
Searching...
No Matches
Public Member Functions | Data Fields | Protected Member Functions
python.gnucash_core.GncNumeric Class Reference
Inheritance diagram for python.gnucash_core.GncNumeric:
python.gnucash_core.GnuCashCoreClass

Public Member Functions

 __init__ (self, *args, **kargs)
 
 __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 518 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 533 of file gnucash_core.py.

533 def __init__(self, *args, **kargs):
534 """Constructor that supports the following formats:
535 * No arguments defaulting to zero: eg. GncNumeric() == 0/1
536 * A integer: e.g. GncNumeric(1) == 1/1
537 * Numerator and denominator intager pair: eg. GncNumeric(1, 2) == 1/2
538 * A floating point number: e.g. GncNumeric(0.5) == 1/2
539 * A floating point number with defined conversion: e.g.
540 GncNumeric(0.5, GNC_DENOM_AUTO,
541 GNC_HOW_DENOM_FIXED | GNC_HOW_RND_NEVER) == 1/2
542 * A string: e.g. GncNumeric("1/2") == 1/2
543 """
544 if 'instance' not in kargs:
545 kargs['instance'] = GncNumeric.__args_to_instance(args)
546 GnuCashCoreClass.__init__(self, [], **kargs)
547

Member Function Documentation

◆ __abs__()

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

Definition at line 705 of file gnucash_core.py.

705 def __abs__(a):
706 """abs(a)"""
707 return a.abs()
708

◆ __bool__()

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

Definition at line 687 of file gnucash_core.py.

687 def __bool__(a):
688 """a != 0"""
689 return bool(a.num())
690

◆ __eq__()

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

Definition at line 683 of file gnucash_core.py.

683 def __eq__(a, b):
684 """a == b"""
685 return a._richcmp(b, a._eq)
686

◆ __float__()

python.gnucash_core.GncNumeric.__float__ (   self)

Definition at line 691 of file gnucash_core.py.

691 def __float__(self):
692 return self.to_double()
693

◆ __ge__()

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

Definition at line 679 of file gnucash_core.py.

679 def __ge__(a, b):
680 """a >= b"""
681 return a._richcmp(b, a._ge)
682

◆ __gt__()

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

Definition at line 671 of file gnucash_core.py.

671 def __gt__(a, b):
672 """a > b"""
673 return a._richcmp(b, a._gt)
674

◆ __int__()

python.gnucash_core.GncNumeric.__int__ (   self)

Definition at line 694 of file gnucash_core.py.

694 def __int__(self):
695 return int(self.to_double())
696

◆ __le__()

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

Definition at line 675 of file gnucash_core.py.

675 def __le__(a, b):
676 """a <= b"""
677 return a._richcmp(b, a._le)
678

◆ __lt__()

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

Definition at line 667 of file gnucash_core.py.

667 def __lt__(a, b):
668 """a < b"""
669 return a._richcmp(b, a._lt)
670

◆ __neg__()

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

Definition at line 701 of file gnucash_core.py.

701 def __neg__(a):
702 """-a"""
703 return a.neg()
704

◆ __pos__()

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

Definition at line 697 of file gnucash_core.py.

697 def __pos__(a):
698 """+a"""
699 return GncNumeric(a.num(), a.denom())
700
The primary numeric class for representing amounts and values.

◆ __str__()

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

Definition at line 713 of file gnucash_core.py.

713 def __str__(self):
714 """Returns a human readable numeric value string as UTF8."""
715 return gnc_numeric_to_string(self.instance)
716

◆ _add()

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

Definition at line 608 of file gnucash_core.py.

608 def _add(a, b):
609 return a.add(b, GNC_DENOM_AUTO, GNC_HOW_RND_ROUND)
610

◆ _div()

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

Definition at line 617 of file gnucash_core.py.

617 def _div(a, b):
618 return a.div(b, GNC_DENOM_AUTO, GNC_HOW_RND_ROUND)
619

◆ _eq()

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

Definition at line 647 of file gnucash_core.py.

647 def _eq(a, b):
648 return a.compare(b) == 0
649

◆ _floordiv()

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

Definition at line 620 of file gnucash_core.py.

620 def _floordiv(a, b):
621 return a.div(b, 1, GNC_HOW_RND_TRUNC)
622

◆ _ge()

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

Definition at line 644 of file gnucash_core.py.

644 def _ge(a, b):
645 return a.compare(b) in (0,1)
646

◆ _gt()

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

Definition at line 638 of file gnucash_core.py.

638 def _gt(a, b):
639 return a.compare(b) == 1
640

◆ _le()

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

Definition at line 641 of file gnucash_core.py.

641 def _le(a, b):
642 return a.compare(b) in (0,-1)
643

◆ _lt()

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

Definition at line 635 of file gnucash_core.py.

635 def _lt(a, b):
636 return a.compare(b) == -1
637

◆ _mul()

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

Definition at line 614 of file gnucash_core.py.

614 def _mul(a, b):
615 return a.mul(b, GNC_DENOM_AUTO, GNC_HOW_RND_ROUND)
616

◆ _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 585 of file gnucash_core.py.

585 def _operator_fallbacks(monomorphic_operator, fallback_operator):
586 """fallbacks are not needed except for method name,
587 keep for possible later use"""
588 def forward(a, b):
589 if isinstance(b, GncNumeric):
590 return monomorphic_operator(a, b)
591 if isinstance(b, (int, float)):
592 return monomorphic_operator(a, GncNumeric(b))
593 else:
594 return NotImplemented
595 forward.__name__ = '__' + fallback_operator.__name__ + '__'
596 forward.__doc__ = monomorphic_operator.__doc__
597
598 def reverse(b, a):
599 if isinstance(a, (GncNumeric, int, float)):
600 return forward(b, a)
601 else:
602 return NotImplemented
603 reverse.__name__ = '__r' + fallback_operator.__name__ + '__'
604 reverse.__doc__ = monomorphic_operator.__doc__
605
606 return forward, reverse
607

◆ _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 650 of file gnucash_core.py.

650 def _richcmp(self, other, op):
651 """Helper for comparison operators, for internal use only.
652 Implement comparison between a GncNumeric instance `self`,
653 and either another GncNumeric instance, an int or a float
654 `other`. If `other` is not an instance of that kind, return
655 NotImplemented. `op` should be one of the six standard
656 comparison operators. The comparisons are based on
657 GncNumeric.compare().
658 """
659 import math
660 if isinstance(other, GncNumeric):
661 return op(other)
662 elif isinstance(other, (int, float)):
663 return op(GncNumeric(other))
664 else:
665 return NotImplemented
666

◆ _sub()

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

Definition at line 611 of file gnucash_core.py.

611 def _sub(a, b):
612 return a.sub(b, GNC_DENOM_AUTO, GNC_HOW_RND_ROUND)
613

◆ to_fraction()

python.gnucash_core.GncNumeric.to_fraction (   self)

Definition at line 709 of file gnucash_core.py.

709 def to_fraction(self):
710 from fractions import Fraction
711 return Fraction(self.num(), self.denom())
712

Field Documentation

◆ instance

python.gnucash_core.GncNumeric.instance

Definition at line 715 of file gnucash_core.py.


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