python float_Python float()

python float

In this tutorial we are going to learn about python float() function. In our previous tutorial we learned about Python Join.

在本教程中,我们将学习python float()函数。 在上一教程中,我们了解了Python Join

Python float() (Python float())

As the name says, python float() function returns a floating point number. It takes one parameter and that is optional. So the basic syntax of python float() function is;

顾名思义,python float()函数返回一个浮点数。 它采用一个参数,并且是可选的。 因此,python float()函数的基本语法为;

Here, item can be string, integer or float. So the basic example about python float is given in the following code.

在这里,项目可以是字符串,整数或浮点数。 因此,以下代码给出了有关python float的基本示例。

# init a string with the value of a number
str_to_float = '12.60'
# check the type of the variable
print('The type of str_to_float is:', type(str_to_float))
# use the float() function
str_to_float = float(str_to_float)
# now check the type of the variable
print('The type of str_to_float is:', type(str_to_float))

print('\n')

# init an integer
int_to_float = 12
# check the type of the variable
print('The type of int_to_float is:', type(int_to_float))
print(int_to_float)
# use the float() function
int_to_float = float(int_to_float)
# now check the type of the variable
print('The type of int_to_float is:', type(int_to_float))
print(int_to_float)

So, if you check the output, you will find

因此,如果您检查输出,您会发现

The type of str_to_float is: <class 'str'>
The type of str_to_float is: <class 'float'>


The type of int_to_float is: <class 'int'>
12
The type of int_to_float is: <class 'float'>
12.0

Python float()特殊参数 (Python float() special parameters)

As we said earlier, the parameter of float() function is optional. So if you print a blank float() function, what will it print?

如前所述, float()函数的参数是可选的。 因此,如果您打印一个空白的float()函数,它将打印什么?

Well, it will print 0.0 as output. Again you can define infinity value in float and also NaN (Not A Number) using this function. To do so, you just have to put a string containing inf or NaN as parameter. Here the following code will guide you about this.

好吧,它将输出0.0作为输出。 同样,您可以使用此函数在float中定义无穷大值,还可以定义NaN(非数字)。 为此,您只需要放入一个包含infNaN作为参数的字符串。 下面的代码将指导您有关此的操作。

# store the valure returned by float()
store = float()
# check the type of the variable
print('The type of variable store is :', type(store))
# print the value stored in the variable
print('The value of store :', store)

print()

# store the infinite value by float()
store = float('inf')
# check the type of the variable
print('The type of variable store is :', type(store))
# print the value stored in the variable
print('The value of store :', store)

print()

# store the 'Not a Number' value by float()
store = float('NaN')
# check the type of the variable
print('The type of variable store is :', type(store))
# print the value stored in the variable
print('The value of store :', store)

And you will get output like this.

这样您将获得输出。

The type of variable store is : <class 'float'>
The value of store : 0.0

The type of variable store is : <class 'float'>
The value of store : inf

The type of variable store is : <class 'float'>
The value of store : nan

So, That’s all about Python float function. Hope that you learned well. For any further query, feel free to use the comment box below.

所以,这一切都与Python float函数有关。 希望你学得好。 对于任何其他查询,请随时使用下面的评论框。

翻译自: https://www.journaldev.com/15178/python-float

python float

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值