c 浮点精度 示例_带有示例的Python浮点数教程

c 浮点精度 示例

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 Digit Count After Point to Show
Set Digit Count After Point to Show
设置要显示的点后位数

将总位数设置为显示(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
LEARN MORE  What Is Binary Code?
了解更多什么是二进制代码?

翻译自: https://www.poftut.com/python-floating-numbers-tutorial-examples/

c 浮点精度 示例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值