如何以5的倍数取整浮点

2024-09-28 22:28:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我得到这个号码1.12412,我想round it to 1.124151.12410 (muliple of 5 in last decimal)

如果使用Round(X,4)函数,我得到1.1241 (4 decimals)

有没有一种功能可以实现这一点? 谢谢

堆栈中有一个答案,但使用c#而不是python


Tags: ofto函数答案in功能堆栈it
2条回答

你可以:

  1. 把你的数字乘以2
  2. 使用Round(X,4)
  3. 将结果除以2
  4. 利润

我的方法是首先指定舍入单位,然后使用如下简单技巧:

import numpy as np
rounding_unit = 0.00005
np.round(1.12412/rounding_unit) * rounding_unit

相关问题 更多 >