计算浮点数中的小数位 [英] Count decimal places in a float

查看:98
本文介绍了计算浮点数中的小数位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python中,我导入了一个数字中小数位长度不同的文件,例如7.2或7.2332 是否可以找出数字中的小数位数,如果可以,如何?我能在 SO 上找到的所有问题都是关于格式化到特定小数位的问题,但这不是我在做什么.

应用程序是使用 qdoublespinbox 并将小数位设置为仅需要的数量.

在上下文中,我能够打印数字,这将打印 7.2 或 5.42422 等.如果命令提示符打印正确的数字(即不打印浮点数的内存分配中的所有内容)有什么办法获取该信息.

解决方案

假设 'import a file' 意味着您的小数是文件中的字符串,您可以使用 reverse 并查找:

<预><代码>>>>f = "7.2332">>>f[::-1].find('.')4>>>f = "7.20">>>f[::-1].find('.')2

In python, I import a file that has different length decimal places in the numbers, such as 7.2 or 7.2332 Is it possible to find out the number of decimal places in the number, if so, how? All the question I can find on SO are about formatting to a specific decimal place, but thats not what I am doing.

The application is to use qdoublespinbox and set the decimal places to only the required amount.

Edit:

In context, I am able to print the number, which will print 7.2, or 5.42422 etc. If the command prompt prints the correct digits (ie. doesnt print everything in the memory allocation of a float) is there any way to get that information.

解决方案

Assuming that 'import a file' means your decimals are string in a file, you can use reverse and find:

>>> f = "7.2332"
>>> f[::-1].find('.')
4
>>> f = "7.20"
>>> f[::-1].find('.')
2

这篇关于计算浮点数中的小数位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆