简体   繁体   English

Django / Python在列表理解期间转换float

[英]Django/Python convert float during list comprehension

I have a Django Queryset object called data containing string keys and float values. 我有一个Django Queryset对象,称为data其中包含字符串键和浮点值。

I want to convert all floats to 2 decimal places. 我想将所有浮点数转换为2个小数位。

If I do this: 如果我这样做:

>>> param1_values = [d[param1] for d in data]
>>> param1_values[:5]
[26.1, 24.6, 26.2444444444444, 27.3103448275862, 26.7576923076923]

...which is fine but I need to float to two decimal places. ...很好,但是我需要浮到两位小数。

However, if I do this: 但是,如果我这样做:

>>> param1_values = ["%.2f" % d['mean_air_temp'] for d in data]
>>> param1_values[:5]
['26.10', '24.60', '26.24', '27.31', '26.76']

The numbers are what I wanted but they are converted to strings! 数字是我想要的,但它们会转换为字符串! Why does this occur and how do I solve it? 为什么会发生这种情况,我该如何解决?

Any help appreciated. 任何帮助表示赞赏。

You can use the built-in round function: 您可以使用内置的round函数:

param1_values = [round(d[param1], 2) for d in data]

Note: The behavior of round() for floats can be surprising: for example, round(2.675, 2) gives 2.67 instead of the expected 2.68. 注意:float()的round()行为可能令人惊讶:例如,round(2.675,2)给出2.67,而不是预期的2.68。 This is not a bug: it's a result of the fact that most decimal fractions can't be represented exactly as a float. 这不是错误:这是由于大多数十进制小数不能完全表示为浮点数的结果。 See Floating Point Arithmetic: Issues and Limitations for more information. 有关更多信息,请参见浮点算法:问题和限制

This seems to be accurate enough for your use case. 对于您的用例,这似乎足够准确。 If you need a more precise answer see Ignacio's response using Decimal. 如果您需要更精确的答案,请使用十进制查看Ignacio的响应。

Due to unavoidable inaccuracies in IEEE 754 floating point representation , it is impossible to solve this while keeping them floats. 由于IEEE 754浮点表示法中不可避免的误差 ,因此无法在保持浮点数的情况下解决该问题。

>>> l = [26.1, 24.6, 26.2444444444444, 27.3103448275862, 26.7576923076923]
>>> [decimal.Decimal(x).quantize(decimal.Decimal('0.1')) for x in l]
[Decimal('26.1'), Decimal('24.6'), Decimal('26.2'), Decimal('27.3'), Decimal('26.8')]

The appropriate field to use in Django if you want to store these is DecimalField (unless you're using SQLite, but that's a whole other kettle of fish). 如果要存储这些字段,则在Django中使用的合适字段是DecimalField (除非您使用的是SQLite,但这是另外一堆鱼)。

Do you really need the values rounded, or are you simply wanting to display two values after the decimal point when displaying the values in a template? 您是否真的需要四舍五入的值,还是在模板中显示值时只想在小数点后显示两个值? If you are working on a display issue, use the 'floatformat' filter . 如果您要处理显示问题,请使用'floatformat'过滤器 Again, if this is what you're trying to accomplish, I believe that as you roll through the list you can use "listitem|floatformat:-2" to get the desired result. 同样,如果这是您要完成的工作,我相信您在滚动列表时可以使用“ listitem | floatformat:-2”获得所需的结果。 If, instead, you wish to store and use the rounded version in Python, you'll not have an easy time of it . 相反,如果您希望在Python中存储和使用四舍五入的版本,您将不会很轻松

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM