python - Python:圆形浮点数保持0
问题描述
我只需要在 Python 中舍入浮点数的最后 2 个正数
前任:
- 0.000000302329303 -> 0.00000031
- 0.09838 -> 0.10
- 0.0003299 -> 0.00033
这可能以像圆形这样的简单方式吗?
解决方案
我认为您的示例是错误的,但只需根据需要调整代码:
# this code doesn't work if some are defined as 0.123 and others as .123
a = 0.000000302329303
nb_characters = len(str(a))
rounding_expr = "%%.%sf" % (nb_characters - 4)
rounded_a = float(rounding_expr % a)
推荐阅读
- javascript - 如何将 Array.prototype 方法作为参数传递给 curried 函数
- python - loadData = "LOAD DATA ' %s ' INTO TABLE %s " %finalpath %tblname 类型错误:格式字符串的参数不足
- css - 动画 div onHover
- ios - 如何改进 Swift / Xcode iOS App 中文本字段的控制语句?
- sql-server - 在 Excel VBA 的单个查询中加入 Access 表和 SQL Server 表
- python-3.x - 通过方法读取 CSV 文件时出现奇怪的错误
- c++ - 有没有办法摆脱这个图形程序中的错误“不推荐使用字符串转换”?
- javascript - 如何垂直和水平居中光滑滑块?
- android - 如何无延迟地使用 MediaPlayer 播放音频?
- jquery - DataTables json 未处理(在 Laravel 中)