本文整理汇总了Python中fractions.Fraction.from_decimal方法的典型用法代码示例。如果您正苦于以下问题:Python Fraction.from_decimal方法的具体用法?Python Fraction.from_decimal怎么用?Python Fraction.from_decimal使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类fractions.Fraction
的用法示例。
在下文中一共展示了Fraction.from_decimal方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: fechar
# 需要导入模块: from fractions import Fraction [as 别名]
# 或者: from fractions.Fraction import from_decimal [as 别名]
def fechar(self):
"""
Torna a venda fechada -- o cliente já pagou a conta.
Cria/sobrescreve a transação de 10% referente à venda.
"""
if self.fechada:
return
if self.transacao_10p:
self.transacao_10p.delete()
if self.gorjeta:
registro_10p = Registro.objects.get(nome=NOME_DO_REGISTRO)
self.transacao_10p = Transacao(
registro=registro_10p,
data=self.dia.data,
descricao="10% a pagar do dia {0}".format(self.dia.data)
)
self.transacao_10p.save()
config = get_config()
a_pagar = Decimal(
float(
Fraction.from_decimal(self.gorjeta) \
* Fraction(9, 10) \
* config.fracao_10p_funcionarios
)
)
self.transacao_10p.lancamentos.create(
valor=a_pagar * -1,
conta=join("entrada", "vendas", "10% funcionarios"),
)
self.transacao_10p.lancamentos.create(
valor=a_pagar,
conta=join("bens", "caixa"),
)
self.transacao_10p.lancamentos.create(
valor=a_pagar,
conta=join("gastos", "funcionarios", "10%"),
)
self.transacao_10p.lancamentos.create(
valor=a_pagar * -1,
conta=join("dividas", "contas a pagar", "10%"),
)
self.fechada = True
self.save()
示例2: __mul__
# 需要导入模块: from fractions import Fraction [as 别名]
# 或者: from fractions.Fraction import from_decimal [as 别名]
def __mul__(self, frac):
"""
Multiply the amount by the given fraction, using bankers rounding
to round to the nearest value
"""
if isinstance(frac, Decimal):
frac = Fraction.from_decimal(frac)
elif not isinstance(frac, Rational):
raise TypeError('Expected a Decimal or a numbers.Rational value')
return MoneyAmount(self.code, frac * self.value)
示例3: prettyprint
# 需要导入模块: from fractions import Fraction [as 别名]
# 或者: from fractions.Fraction import from_decimal [as 别名]
def prettyprint(unit):
"""
Convert from decimal to a more friendly recipe-ish way
like instead of 2.2 cups to 2 + 1/5 cups
"""
int_part = int(floor(unit))
if float(unit - int_part) <= 0.1:
return str(int_part)
r_part = str(Fraction.from_decimal(unit - int_part).limit_denominator(5))
if r_part == "1":
return str(int_part + 1)
if int_part == 0:
return "%s" % (r_part)
return "%s + %s" % (int_part, r_part)
示例4: get_den
# 需要导入模块: from fractions import Fraction [as 别名]
# 或者: from fractions.Fraction import from_decimal [as 别名]
def get_den(x):
fr = Fraction.from_decimal(Decimal(x).sqrt())
return fr.limit_denominator(1000000000000).denominator
示例5: __new__
# 需要导入模块: from fractions import Fraction [as 别名]
# 或者: from fractions.Fraction import from_decimal [as 别名]
def __new__(cls, numerator=0, denominator=None):
self = super(AAFFraction, cls).__new__(cls)
if denominator is None:
if isinstance(numerator, Rational):
self._numerator = numerator.numerator
self._denominator = numerator.denominator
return self
elif isinstance(numerator, float):
# Exact conversion from float
value = Fraction.from_float(numerator)
self._numerator = value._numerator
self._denominator = value._denominator
return self
elif isinstance(numerator, Decimal):
value = Fraction.from_decimal(numerator)
self._numerator = value._numerator
self._denominator = value._denominator
return self
elif isinstance(numerator, str):
# Handle construction from strings.
m = _RATIONAL_FORMAT.match(numerator)
if m is None:
raise ValueError('Invalid literal for Fraction: %r' %
numerator)
numerator = int(m.group('num') or '0')
denom = m.group('denom')
if denom:
denominator = int(denom)
else:
denominator = 1
decimal = m.group('decimal')
if decimal:
scale = 10**len(decimal)
numerator = numerator * scale + int(decimal)
denominator *= scale
exp = m.group('exp')
if exp:
exp = int(exp)
if exp >= 0:
numerator *= 10**exp
else:
denominator *= 10**-exp
if m.group('sign') == '-':
numerator = -numerator
else:
raise TypeError("argument should be a string "
"or a Rational instance")
elif (isinstance(numerator, Rational) and
isinstance(denominator, Rational)):
numerator, denominator = (
numerator.numerator * denominator.denominator,
denominator.numerator * numerator.denominator
)
else:
raise TypeError("both arguments should be "
"Rational instances")
if denominator == 0:
raise ZeroDivisionError('Fraction(%s, 0)' % numerator)
# don't find the gcd
#g = gcd(numerator, denominator)
self._numerator = numerator
self._denominator = denominator
return self
示例6: __init__
# 需要导入模块: from fractions import Fraction [as 别名]
# 或者: from fractions.Fraction import from_decimal [as 别名]
def __init__(cls, numerator=0, denominator=1):
"""Constructs a Rational.
Takes a string like '3/2' or '1.5', another Rational instance, a
numerator/denominator pair, or a float.
Examples
--------
>>> Fraction(10, -8)
Fraction(-5, 4)
>>> Fraction(Fraction(1, 7), 5)
Fraction(1, 35)
>>> Fraction(Fraction(1, 7), Fraction(2, 3))
Fraction(3, 14)
>>> Fraction('314')
Fraction(314, 1)
>>> Fraction('-35/4')
Fraction(-35, 4)
>>> Fraction('3.1415') # conversion from numeric string
Fraction(6283, 2000)
>>> Fraction('-47e-2') # string may include a decimal exponent
Fraction(-47, 100)
>>> Fraction(1.47) # direct construction from float (exact conversion)
Fraction(6620291452234629, 4503599627370496)
>>> Fraction(2.25)
Fraction(9, 4)
>>> Fraction(Decimal('1.47'))
Fraction(147, 100)
"""
self = super(Rational, cls).__init__
if denominator is 1:
if isinstance(numerator, numbers.Rational):
self._numerator = numerator.numerator
self._denominator = numerator.denominator
return self
elif isinstance(numerator, float):
# Exact conversion from float
value = Fraction.from_float(numerator)
self._numerator = value._numerator
self._denominator = value._denominator
return self
elif isinstance(numerator, Decimal):
value = Fraction.from_decimal(numerator)
self._numerator = value._numerator
self._denominator = value._denominator
return self
elif isinstance(numerator, str):
# Handle construction from strings.
m = _RATIONAL_FORMAT.match(numerator)
if m is None:
raise ValueError('Invalid literal for Fraction: %r' %
numerator)
numerator = int(m.group('num') or '0')
denom = m.group('denom')
if denom:
denominator = int(denom)
else:
denominator = 1
decimal = m.group('decimal')
if decimal:
scale = 10**len(decimal)
numerator = numerator * scale + int(decimal)
denominator *= scale
exp = m.group('exp')
if exp:
exp = int(exp)
if exp >= 0:
numerator *= 10**exp
else:
denominator *= 10**-exp
if m.group('sign') == '-':
numerator = -numerator
else:
raise TypeError("argument should be a string "
"or a Rational instance")
elif (isinstance(numerator, numbers.Rational) and
isinstance(denominator, numbers.Rational)):
numerator, denominator = (
numerator.numerator * denominator.denominator,
denominator.numerator * numerator.denominator
)
else:
raise TypeError("both arguments should be "
"Rational instances")
if denominator == 0:
raise ZeroDivisionError('Fraction(%s, 0)' % numerator)
g = gcd(numerator, denominator)
self._numerator = numerator // g
self._denominator = denominator // g
return self
示例7: decimal_to_num_denom
# 需要导入模块: from fractions import Fraction [as 别名]
# 或者: from fractions.Fraction import from_decimal [as 别名]
def decimal_to_num_denom(n):
to_fraction = Fraction.from_decimal(Decimal(n))
numerator, denominator = to_fraction.numerator, to_fraction.denominator
return {'numerator': numerator, 'denominator': denominator}
示例8: decimal_to_fraction
# 需要导入模块: from fractions import Fraction [as 别名]
# 或者: from fractions.Fraction import from_decimal [as 别名]
def decimal_to_fraction(n):
return Fraction.from_decimal(Decimal(n))