next up previous
Next: FixedPoint Rounding Up: libfixedpoint Previous: libfixedpoint

FixedPoint -- Fixed decimal object with settable precision and user definable rounding

standardFixedPoint

Tim Peterstim.one@comcast.net Doug Fortdougfort@dougfort.net

Joe Griffinjoeg@downright.com

FixedPoint objects provide support for fixed decimal arithmetic

The fixedpoint module defines the FixedPoint class which provides a fixed decimal data type that supports python operators and standard functions. FixedPoint objects are useful when computing financial transactions where precision is critical.



When FixedPoint objects of different precision are combined via + - * /, the result is computed to the larger of the inputs' precisions, which also becomes the precision of the resulting FixedPoint object.

 
>>> print FixedPoint("3.42") + FixedPoint("100.005", 3)
103.425
>>>

When a FixedPoint is combined with other numeric types (ints, floats, strings representing a number) via + - * /, then similarly the computation is carried out using- and the result inherits -the FixedPoint's precision.

  
>>> print FixedPoint(1) / 7
0.14
>>> print FixedPoint(1, 30) / 7
0.142857142857142857142857142857
>>>

The string produced by str(x) (implicitly invoked by "print") always contains at least one digit before the decimal point, followed by a decimal point, followed by exactly x.get_precision() digits. If x is negative, str(x)[0] == "-".

Note that conversion of floats to FixedPoint can be surprising, and should be avoided whenever possible. Conversion from string is exact (up to final rounding to the requested precision), so is greatly preferred.

 
>>> print FixedPoint(1.1e30)
1099999999999999993725589651456.00
>>> print FixedPoint("1.1e30")
1100000000000000000000000000000.00
>>>

FixedPoint objects have the following public methods:









The fixedpoint module also defines the following (private) functions:













The fixedpoint module exports the following data item(s):





Subsections
next up previous
Next: FixedPoint Rounding Up: libfixedpoint Previous: libfixedpoint
Ganesan R 2003-08-13