當前位置: 首頁>>代碼示例>>Python>>正文


Python decimal.Inexact方法代碼示例

本文整理匯總了Python中decimal.Inexact方法的典型用法代碼示例。如果您正苦於以下問題:Python decimal.Inexact方法的具體用法?Python decimal.Inexact怎麽用?Python decimal.Inexact使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在decimal的用法示例。


在下文中一共展示了decimal.Inexact方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: float_to_decimal

# 需要導入模塊: import decimal [as 別名]
# 或者: from decimal import Inexact [as 別名]
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 
開發者ID:dwolfhub,項目名稱:zxcvbn-python,代碼行數:13,代碼來源:time_estimates.py

示例2: float_to_decimal

# 需要導入模塊: import decimal [as 別名]
# 或者: from decimal import Inexact [as 別名]
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 
開發者ID:venmo,項目名稱:business-rules,代碼行數:17,代碼來源:utils.py

示例3: to_xdr_amount

# 需要導入模塊: import decimal [as 別名]
# 或者: from decimal import Inexact [as 別名]
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 
開發者ID:StellarCN,項目名稱:py-stellar-base,代碼行數:52,代碼來源:operation.py


注:本文中的decimal.Inexact方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。