MCPcopy Index your code
hub / github.com/python/cpython / create_decimal_from_float

Method create_decimal_from_float

Lib/_pydecimal.py:4092–4107  ·  view source on GitHub ↗

Creates a new Decimal instance from a float but rounding using self as the context. >>> context = Context(prec=5, rounding=ROUND_DOWN) >>> context.create_decimal_from_float(3.1415926535897932) Decimal('3.1415') >>> context = Context(prec=5, traps=[Inexact])

(self, f)

Source from the content-addressed store, hash-verified

4090 return d._fix(self)
4091
4092 def create_decimal_from_float(self, f):
4093 """Creates a new Decimal instance from a float but rounding using self
4094 as the context.
4095
4096 >>> context = Context(prec=5, rounding=ROUND_DOWN)
4097 >>> context.create_decimal_from_float(3.1415926535897932)
4098 Decimal('3.1415')
4099 >>> context = Context(prec=5, traps=[Inexact])
4100 >>> context.create_decimal_from_float(3.1415926535897932)
4101 Traceback (most recent call last):
4102 ...
4103 decimal.Inexact: None
4104
4105 """
4106 d = Decimal.from_float(f) # An exact conversion
4107 return d._fix(self) # Apply the context rounding
4108
4109 # Methods
4110 def abs(self, a):

Callers 2

test_float_operationMethod · 0.80

Calls 2

_fixMethod · 0.80
from_floatMethod · 0.45

Tested by 2

test_float_operationMethod · 0.64