简体   繁体   English

判断数字是否为小数。python中的小数类型

[英]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)变量“a”的类型可以是 - int/float/decimal.Decimal(但不是字符串)

I want to check if its a decimal.Decimal type.我想检查它是否是 decimal.Decimal 类型。

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.谢谢。

Use isinstance which outputs True or False .使用输出TrueFalseisinstance

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python decimal.Decimal - getcontext().prec 设置和输出中的小数位数 - Python decimal.Decimal - getcontext().prec setting and number of decimals in output 使decimal.Decimal成为Python中的默认数值类型 - Making decimal.Decimal the default numerical type in Python - 不支持的操作数类型:Python 中的“decimal.Decimal”和“float”3 - unsupported operand type(s) for -: 'decimal.Decimal' and 'float' in Python 3 Excel 数据未按 decimal.Decimal 类型排序 - Excel data not sorting the type decimal.Decimal Python十进制。十进制ID不一样 - Python decimal.Decimal id not the same 如何创建具有给定数量的有效数字的 decimal.Decimal 对象? - How to create a decimal.Decimal object with a given number of significant figures? Django的:“decimal.Decimal”和“字典”:为+不受支持的操作数类型 - Django: unsupported operand type(s) for +: 'decimal.Decimal' and 'dict' InterfaceError未知类型 <class 'decimal.Decimal'> 对于参数10 - InterfaceError unknown type <class 'decimal.Decimal'> for arg 10 Pandas `pivot_table` 使用 `decimal.Decimal` 类型 - Pandas `pivot_table` working with `decimal.Decimal` type Pymongo:无法编码十进制类型的 object。十进制? - Pymongo: Cannot encode object of type decimal.Decimal?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM