Pranav Joshi
Pranav Joshi

Reputation: 147

Checking if the number is a decimal.Decimal type in python

Variable 'a' could be of type - int/float/decimal.Decimal (but not a string)

I want to check if its a decimal.Decimal type.

Following works:

import decimal
a = decimal.Decimal(4)

if type(a) is decimal.Decimal:
    print('yes decimal')
else: 
    print('not decimal')

But, is there a righter way of doing the same? tnx.

Upvotes: 5

Views: 5769

Answers (1)

ML_Enthu
ML_Enthu

Reputation: 304

Use isinstance which outputs True or False.

result = isinstance(a, decimal.Decimal)
print(result)

Upvotes: 6

Related Questions