gpt4 book ai didi

python - X 的浮点精度

转载 作者:行者123 更新时间:2023-11-30 23:31:17 25 4
gpt4 key购买 nike

这不是代码修订版,因此我在这里创建了线程。我有一个作业如下:

Write a program which: 1. intializes a variable a with 123, and prints the value of a over 7 places, 2. intializes a variable b with -89.6548754, and prints the value of b over 10 places and with a floating point precision of 4

第一个是:

a = 123
print("The value is {0:7d}".format(a))

我不明白的是第二个。什么是“浮点精度为 4”?这是什么意思?我猜这意味着将小数位数(即 7 (6548754))缩短为 4。

b=-89.6548754
print("The value is {:10.4f}".format(b))

最佳答案

来自format string mini-language specification :

The precision is a decimal number indicating how many digits should be displayed after the decimal point for a floating point value formatted with 'f' and 'F', or before and after the decimal point for a floating point value formatted with 'g' or 'G'. For non-number types the field indicates the maximum field size - in other words, how many characters will be used from the field content. The precision is not allowed for integer values.

所以是的,它是小数点后的位数。

关于python - X 的浮点精度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20027040/

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