gpt4 book ai didi

python - `G` 类型的python % 字符串格式如何表现?

转载 作者:行者123 更新时间:2023-12-02 11:19:42 26 4
gpt4 key购买 nike

read :

The g and G conversion types choose between floating point or exponential output, depending on the magnitude of the exponent and the value specified for .<precision>. Output is the same as e/E if the exponent is less than -4 or not less than .<precision>. Otherwise, it’s the same as f/F:


另外,我没有得到“如果指数小于 -4 或不小于 .<precision>”。我们在哪里明确指定指数? “指数”是指小数点后的位数吗?我觉得是这样,因为当我尝试在小数点后保留四位数字时,它打印为 f ,但是当我尝试在小数点后保留五位数字时,它打印为 e :
>>> '%g' % 0.0003
'0.0003'
>>> '%g' % 0.00003
'3e-05'
一季度。 我对上述理解正确吗?
Q2。 但是,然后,我不明白为什么它在下面的例子中没有做同样的事情:
>>> '%g' % 3.14
'3.14'
>>> '%g' % 3.014
'3.014'
>>> '%g' % 3.0014
'3.0014'
>>> '%g' % 3.00014
'3.00014'
>>> '%g' % 3.000014
'3.00001'
>>> '%g' % 3.0000014
'3'
Q3。 最后,我没有得到“如果指数小于 -4 或不小于 .<precision>”的“或不小于 .<precision>”部分。我试图明确指定小数点后小于和大于 4 位的精度。但它仍然打印相同的内容:
>>> '%g' % 0.00003
'3e-05'
>>> '%.3g' % 0.00003
'3e-05'
>>> '%.9g' % 0.00003
'3e-05'

最佳答案

“指数”是指小数点后的位数吗?
不。指数只是说你在 e 之后得到的数字在符号中,例如 0.999可能呈现为 9.990000e-01表示 0.999 的指数是 -1 .指数值确实告知数量级。

关于python - `G` 类型的python % 字符串格式如何表现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64239946/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com