X的浮点精度

2024-06-16 10:59:20 发布

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

这不是代码修订版,因此我在这里创建了线程。我的任务如下:

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))

Tags: andofthe代码formatisvaluewith
1条回答
网友
1楼 · 发布于 2024-06-16 10:59:20

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.

是的,它是小数点后的位数。你知道吗

相关问题 更多 >