简体   繁体   English

如何找到具有两位以上小数的浮点数? (Python)

[英]How can I find float numbers that have more than two decimals? (Python)

I need a solution to access all numbers that have more than two decimals.我需要一个解决方案来访问所有小数点后两位以上的数字。 eg例如

Have:有:

nums = [0.95, 0.7, 0.0, 0.3234, 0.54563]

Need:需要:

many_decimals = [0.3234, 0.54563]

Thanks a lot:)非常感谢:)

You can convert to string, reverse the string, then get index of '.'您可以转换为字符串,反转字符串,然后获取 '.' 的索引in the string which is the count of decimal places.在字符串中,它是小数位数。

Try something like this:尝试这样的事情:

nums = [0.95, 0.7, 0.0, 0.3234, 0.54563]
print([f for f in nums if str(f)[::-1].find('.') > 2])

Output:输出:

[0.3234, 0.54563]

Solution breakdown:解决方案分解:

s = str(0.54563)[::-1] # => '36545.0'
s.find('.')            # => 5

An alternative can use the decimal package and use the exponent, which creates the same output as above.另一种方法可以使用 decimal 包并使用 exponent,它创建与上述相同的输出。

import decimal
print([f for f in nums if abs(decimal.Decimal(str(f)).as_tuple().exponent) > 2])

10**x*n%1>0 will return True if n has more than x decimals 10**x*n%1>0如果 n 有超过 x 个小数,将返回 True

so you can do many_decimals = [n for n in nums if 10**2*n%1>0]所以你可以做many_decimals = [n for n in nums if 10**2*n%1>0]

Explanation解释

10**x*n%1>0 is ((10**x) * n) % 1 > 0 : 10**x*n%1>0((10**x) * n) % 1 > 0

  1. 10 to the power of x times n will make decimals shift x spaces to the left 10 的 x 次方 n 将使小数向左移动 x 个空格
  2. modulo 1 of that returns only the decimal part in python, like 213.123 % 1 = 0.123其中模 1 仅返回 python 中的小数部分,如 213.123 % 1 = 0.123
  3. if it's not 0 that means that it had more than x decimals如果它不是 0,则意味着它有超过 x 个小数

The fraction of the exact float number's value must end with: 0.0, 0.25, 0.5, 0.75 .精确浮点数的小数部分必须以: 0.0, 0.25, 0.5, 0.75结尾。

Algorithm:算法:

  1. Extract the fraction.提取分数。
  2. Scaled by 4.0.按 4.0 缩放。
  3. Test fraction against 0.0.针对 0.0 的测试分数。

If non-zero, the original value needs more than 2 places for exact decimal representation.如果非零,则原始值需要多于 2 位才能精确表示十进制。

暂无
暂无

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

相关问题 在 python 中,我怎样才能有一个带有两个小数位的浮点数(作为浮点数)? - In python how can I have a float with two decimal places (as a float)? 如何制作一个 python 计算器,它可以对两个以上的数字进行加、减、乘、除、幂和取模 - How to make a python calculator with that can add , subtract , multiply , divide , powers and modulo on more more than two numbers 如何在Pyspark中将数据框的浮点类型列定为不超过1个小数? - How can I delimit a Float type column of a dataframe to have no more than 1 decimal in Pyspark? 如何在python中找到大小大于值的组? - How can I find the groups with size more than a value in python? 浮点数应有5个前导数字且无小数 - Float should have 5 leading numbers and no decimals 如何在Python中具有浮点数列表的2个单元格之间寻找容差相等? - How can I look for equality with tolerance between 2 cells who have lists of float numbers in Python? Python - 如何获得两位小数中最大的小数 - Python - How can I get the largest decimal of two decimals 当我在Python中有两个以上的组时,如何进行精确的Fisher测试? - How to conduct an exact Fisher test when I have more than two groups in Python? 如何在Python的整个代码中将所有浮点值四舍五入为小数点后1位 - How can I round all float values into 1 decimals during the whole code in Python Pandas Dataframe,当某些行可能超过1“时,如何将列拆分为”,“ - Pandas Dataframe, How can I split a column into two by “,” when some rows may have more than 1 “,”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM