简体   繁体   English

在 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

More details: https://kite.com/python/answers/how-to-print-a-number-in-scientific-notation-in-python更多详细信息: https://kite.com/python/answers/how-to-print-a-number-in-scientific-notation-in-python

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以获得舍入数。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM