Python decimal.Inexact() Examples

The following are 3 code examples of decimal.Inexact(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module decimal , or try the search function .
Example #1
Source File: time_estimates.py    From zxcvbn-python with MIT License 5 votes vote down vote up
def float_to_decimal(f):
    "Convert a floating point number to a Decimal with no loss of information"
    n, d = f.as_integer_ratio()
    numerator, denominator = Decimal(n), Decimal(d)
    ctx = Context(prec=60)
    result = ctx.divide(numerator, denominator)
    while ctx.flags[Inexact]:
        ctx.flags[Inexact] = False
        ctx.prec *= 2
        result = ctx.divide(numerator, denominator)
    return result 
Example #2
Source File: utils.py    From business-rules with MIT License 5 votes vote down vote up
def float_to_decimal(f):
    """
    Convert a floating point number to a Decimal with
    no loss of information. Intended for Python 2.6 where
    casting float to Decimal does not work.
    """
    n, d = f.as_integer_ratio()
    numerator, denominator = Decimal(n), Decimal(d)
    ctx = Context(prec=60)
    result = ctx.divide(numerator, denominator)
    while ctx.flags[Inexact]:
        ctx.flags[Inexact] = False
        ctx.prec *= 2
        result = ctx.divide(numerator, denominator)
    return result 
Example #3
Source File: operation.py    From py-stellar-base with Apache License 2.0 4 votes vote down vote up
def to_xdr_amount(value: Union[str, Decimal]) -> int:
        """Converts an amount to the appropriate value to send over the network
        as a part of an XDR object.

        Each asset amount is encoded as a signed 64-bit integer in the XDR
        structures. An asset amount unit (that which is seen by end users) is
        scaled down by a factor of ten million (10,000,000) to arrive at the
        native 64-bit integer representation. For example, the integer amount
        value 25,123,456 equals 2.5123456 units of the asset. This scaling
        allows for seven decimal places of precision in human-friendly amount
        units.

        This static method correctly multiplies the value by the scaling factor
        in order to come to the integer value used in XDR structures.

        See `Stellar's documentation on Asset Precision
        <https://www.stellar.org/developers/guides/concepts/assets.html#amount-precision-and-representation>`_
        for more information.

        :param value: The amount to convert to an integer for XDR
            serialization.

        """
        if not (isinstance(value, str) or isinstance(value, Decimal)):
            raise TypeError(
                "Value of type '{}' must be of type {} or {}, but got {}.".format(
                    value, str, Decimal, type(value)
                )
            )
        # throw exception if value * ONE has decimal places (it can't be represented as int64)
        try:
            amount = int(
                (Decimal(value) * Operation._ONE).to_integral_exact(
                    context=Context(traps=[Inexact])
                )
            )
        except decimal.Inexact:
            raise ValueError(
                "Value of '{}' must have at most 7 digits after the decimal.".format(
                    value
                )
            )

        if amount < 0 or amount > 9223372036854775807:
            raise ValueError(
                "Value of '{}' must represent a positive number "
                "and the max valid value is 922337203685.4775807.".format(value)
            )

        return amount