📜  Python|十进制余数_near() 方法

📅  最后修改于: 2022-05-13 01:55:47.959000             🧑  作者: Mango

Python|十进制余数_near() 方法

Decimal#remainder_near():remainder_near()是一个 Decimal 类方法,它返回 x – y * n,其中 n 是最接近 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