MCPcopy Index your code
hub / github.com/RustPython/RustPython / create_decimal_from_float

Method create_decimal_from_float

Lib/_pydecimal.py:4089–4104  ·  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

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