简体   繁体   English

Python - 如何获得两位小数中最大的小数

[英]Python - How can I get the largest decimal of two decimals

I have two values of type decimal ie <class 'decimal.Decimal'> and <class 'decimal.Decimal'> and the numbers are我有两个十进制类型的值,即<class 'decimal.Decimal'> and <class 'decimal.Decimal'>数字是

print(option.principal.amount, 'and', max_monthly_amount.amount)

Outputs输出

500000.00 and 500000

Getting max of the two values like this像这样获得两个值的最大值

option.principal.amount.max(max_monthly_amount.amount)

Returns退货

'decimal.Decimal' object has no attribute 'max_term'

max(option.principal.amount, max_monthly_amount.amount))

Here's the docs to the standard library function这是标准库 function 的文档

you should convert both of them to float and then use max function this way:您应该将它们都转换为浮点数,然后以这种方式使用 max function :

num1 = float(option.principal.amount)
num2 = float(max_monthly_amount.amount)
print(max(num1, num2))

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

相关问题 如何获得最大可能的精度? (Python - 十进制) - How to get largest possible precision? (Python - Decimal) 我怎样才能将一个小数点后一位的值四舍五入到小数点后两位 - how can I round a value with 1 decimal to 2 decimals 如何让Python在int输入上工作,将它们转换为两个小数点? - How can I get Python to work on int inputs, converting them to two decimal points? 在 python 中,我怎样才能有一个带有两个小数位的浮点数(作为浮点数)? - In python how can I have a float with two decimal places (as a float)? 如何在可变小数位数的python中格式化小数 - How to format a decimal in python with variable number of decimals 如何找到具有两位以上小数的浮点数? (Python) - How can I find float numbers that have more than two decimals? (Python) 在 Python 3.x 中显示到两个小数点……但也没有这些小数点(如果适用) - 如何? - Displaying to two decimal points in Python 3.x… but also without these decimals, if applicable - how? 如何在Python中获得更精确的十进制值 - How can I get more exact decimal values in Python 我如何在python中准确获得2位小数 - How can i get exactly 2 decimal position in python 如何从python中的列表列表中获取n个最大列表 - How can I get n largest lists from a list of lists in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM