Python|十进制 next_toward() 方法
Decimal#next_toward() :next_toward()是一个 Decimal 类方法,它返回最接近第二个十进制值的方向上的第一个十进制值的数字。
Syntax: Decimal.next_toward()
Parameter: Decimal values
Return: the number closest to first decimal value in the direction towards second decimal value.
代码 #1:next_toward() 方法的示例
# Python Program explaining
# next_toward() 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.next_toward() method
print ("\n\nDecimal a with next_toward() method : ", a.next_toward(b))
print ("Decimal b with next_toward() method : ", b.next_toward(b))
输出 :
Decimal value a : -1
Decimal value b : 0.142857
Decimal a with next_toward() method : -0.9999999999999999999999999999
Decimal b with next_toward() method : 0.142857
代码 #2:next_toward() 方法的示例
# Python Program explaining
# next_toward() 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.next_toward() method
print ("\n\nDecimal a with next_toward() method : ", a.next_toward(b))
print ("Decimal b with next_toward() method : ", b.next_toward(b))
输出 :
Decimal value a : -3.14
Decimal value b : 3.21E+7
Decimal a with next_toward() method : -3.139999999999999999999999999
Decimal b with next_toward() method : 3.21E+7