Python‎ > ‎decimal‎ > ‎

Decimal.compare()


Introduction
Compare the Decimal values between Decimals and return with True or False.

Syntax
Decimal(x).compare(y)

Parameters
x and y= Decimal
Required argument two arguments

Examples
 >>> x=Decimal(1/44)
>>> x
Decimal('0.02272727272727272790353
5809446111670695245265960693359375')
>>> y=Decimal(3/88)
>>> y
Decimal('0.034090909090909088
3858567622155533172190189361572265625')
>>> x>y
False
>>> x==y
False
>>> 
>>> Decimal(x).compare(y)
Decimal('-1')

input(decimal().compare())
The method return decimal values in various decimal methods through input() method.
from decimal import Decimal

print("Python Decimal Methods")
a=Decimal(input("Enter Decimal Value: "))

print("Your value in Decimal().adjusted()) method is: ", Decimal(a).adjusted())

print("---------------------------------------------------------------------------------")
print("While other methods return your value as: ")
print("Your value in Decimal().as_tuple(() method is: ", Decimal(a).as_tuple())

print("---------------------------------------------------------------------------------")
print("Your value in Decimal().canonical() method is: ", Decimal(a).canonical())

print("---------------------------------------------------------------------------------")
print("Your value in Decimal().compare(your value-1) method is: ", Decimal(a).compare(a-1))

print("---------------------------------------------------------------------------------")
print("Your value in Decimal().compare_signal(3)) method is: ", Decimal(a).compare_signal(3))

>>> 
Python Decimal Methods
Enter Decimal Value: 55
Your value in Decimal().adjusted()) method is: 1
---------------------------------------------------------------------------------
While other methods return your value as: 
Your value in Decimal().as_tuple(() method is: DecimalTuple(sign=0, digits=(5, 5), exponent=0)
---------------------------------------------------------------------------------
Your value in Decimal().canonical() method is: 55
---------------------------------------------------------------------------------
Your value in Decimal().compare(your value-1) method is: 1
---------------------------------------------------------------------------------
Your value in Decimal().compare_signal(3)) method is: 1
>>>


Comments