Python|十进制余数_near() 方法
Decimal#remainder_near():remainder_near()是一个 Decimal 类方法,它返回 x – y * n,其中 n 是最接近 x / y 精确值的整数
Syntax: Decimal.remainder_near()
Parameter: Decimal values
Return: x – y * n, where n is the integer nearest the exact value of x / y
代码 #1: remainder_near()
方法的示例
# Python Program explaining
# remainder_near() method
# loading decimal library
from decimal import *
# Initializing a decimal value
a = Decimal(-1)
b = Decimal('0.142857')
# printing Decimal values
print ("Decimal value a : ", a)
print ("Decimal value b : ", b)
# Using Decimal.remainder_near() method
print ("\n\nDecimal a with remainder_near() method : ", a.remainder_near(b))
print ("Decimal b with remainder_near() method : ", b.remainder_near(b))
输出 :
Decimal value a : -1
Decimal value b : 0.142857
Decimal a with remainder_near() method : -0.000001
Decimal b with remainder_near() method : 0.000000
代码 #2: remainder_near()
方法的示例
# Python Program explaining
# remainder_near() method
# loading decimal library
from decimal import *
# Initializing a decimal value
a = Decimal('-3.14')
b = Decimal('321e + 5')
# printing Decimal values
print ("Decimal value a : ", a)
print ("Decimal value b : ", b)
# Using Decimal.remainder_near() method
print ("\n\nDecimal a with remainder_near() method : ", a.remainder_near(b))
print ("Decimal b with remainder_near() method : ", b.remainder_near(b))
输出 :
Decimal value a : -3.14
Decimal value b : 3.21E+7
Decimal a with remainder_near() method : -3.14
Decimal b with remainder_near() method : 0E+5