在 python 中将小分数（如 2.234556440301284e-100）舍入为 2.23e-100

[英]Rounding small fraction like 2.234556440301284e-100 to 2.23e-100 in python

I'm trying to print a very small p-value to a plot `(p-value = 4.3289045262877305e-11)` .我正在尝试将一个非常小的 p 值打印到 plot `(p-value = 4.3289045262877305e-11)` I need to round it to have fewer digits after the decimal so it will fit `(4.33e-11)` but when I'm using `round(,3)` it returns `0.0` .我需要将它四舍五入以减少小数点后的位数，以便它适合`(4.33e-11)`但是当我使用`round(,3)`时它返回`0.0` How can I return the number like this `4.33e-11` ?我怎样才能返回这样的数字`4.33e-11`

When you use round you are changing the value and in your example rounding to the nearest thousandth.当您使用round时，您正在更改值，并且在您的示例中四舍五入到最接近的千分之一。 You only want to change the display format.您只想更改显示格式。 "{:.2e}".format(4.3289045262877305e-11) will create a string with the representation you want. "{:.2e}".format(4.3289045262877305e-11) 将创建一个具有您想要的表示形式的字符串。 4.33e-11 4.33e-11

When using `round(,3)` you are rounding up the actual value to `0,00` , not the displayed value.使用`round(,3)`时，您将实际值四舍五入为`0,00` ，而不是显示的值。 If you actually need to round the value itself and not the displayed one, I would suggest you to multiply it by `e11` , then `round (,3)` , then devide it by `e11` to get the rounded number.如果您实际上需要舍入值本身而不是显示的值，我建议您将其乘以`e11` ，然后`round (,3)` ，然后将其除以`e11`以获得舍入数。