c 浮点精度 示例
Numbers are important part of the applications development. We use different type of numbers in different situations. We use int
for integers where there is no floating point. We can use floating point type for floating point numbers. In this tutorial we will learn floating numbers.
数字是应用程序开发的重要组成部分。 我们在不同情况下使用不同类型的数字。 我们将int
用于没有浮点的整数。 我们可以将浮点类型用于浮点数。 在本教程中,我们将学习浮点数。
定义浮点数 (Define Floating Point Number)
We can define floating point number like other variable types in Python but we should also add point .
between decimal and floating part like below.
我们可以像Python中的其他变量类型一样定义浮点数,但也应该添加point .
在小数和浮动部分之间,如下所示。
a=1.1
b = 0.333
c = 12312.34346
精确 (Precision)
Precision means how many digits are used in floating point part. If there are a lot of digits it is called high precision like below.
精度表示在浮点部分中使用多少个数字。 如果有很多数字,则称为高精度,如下所示。
a = 12.353546656576778
If the digits are less in the floating point are we call it low precision.
如果浮点中的位数较少,我们称之为低精度。
a = 1.33
格式化浮点数 (Format Floating Point Number)
As we learn that high precision floating point numbers can have a lot of digits which may be unnecessary for most of the situations like printing and listing. In this situations we should only show some meaningful part of the floating point number. We will use format
function for this.
当我们了解到高精度浮点数可以有很多数字时,对于大多数情况,例如打印和列表,这可能是不必要的。 在这种情况下,我们应该只显示浮点数的一些有意义的部分。 我们将为此使用format
功能。
设置要显示的点后位数 (Set Digit Count After Point to Show)
We can also set total digit count before and after point to show. This is very useful if we have a textbox which have limited character length to show. We will use .10g
parameter in order to show total 10 digits which includes before point.
我们还可以设置要显示的点之前和之后的总位数。 如果我们的文本框显示的字符长度有限,这将非常有用。 我们将使用.10g
参数来显示总共10位数字,其中包括前一点。
pi = 3.141592653589793
format( pi , '.10g')
#'3.141592654'
将总位数设置为显示(Set Total Digit Count to Show)
We can set digit count to show after point. This will made listing floating point numbers more clearly. In this example we will only show 2
digits after point.
我们可以设置位数来显示点之后。 这将使列出浮点数更加清晰。 在此示例中,我们将仅在点后显示2
位数字。
format( pi , '.2f')
#3.14
翻译自: https://www.poftut.com/python-floating-numbers-tutorial-examples/
c 浮点精度 示例