解析要在Python中浮动的字符串(float()函数)

Given a string value (that contains float value) and we have to convert it into float value in Python.

给定一个字符串值(包含浮点值),我们必须在Python中将其转换为浮点值。

To convert a string value to the float, we use float() function.

要将字符串值转换为float ,我们使用float()函数

Python float()函数 (Python float() function)

float() function is a library function in python, it is used to convert a given string or integer value to the float value.

float()函数是python中的库函数,用于将给定的字符串或整数值转换为float值。

Syntax:

句法:

    float(string_value/integer_value)

Example:

例:

    Input:
    str = "10.23"
    print(float(str))
    Output:
    10.23

    Input:
    str = "1001"
    print(float(str))
    Output:
    1001.0

Python code to convert string to the float value

Python代码将字符串转换为浮点值

# python code to demonstrate example of 
# float() function

str1 = "10.23"
str2 = "1001"

# printing str1 & str2 types
print("type of str1: ", type(str1))
print("type of str2: ", type(str2))

# converting to float value
val1 = float(str1)
val2 = float(str2)

# printing types and values of val1 & val2
print("type of val1: ", type(val1))
print("type of val2: ", type(val2))

print("val1 = ", val1)
print("val2 = ", val2)

Output

输出量

type of str1:  <class 'str'>
type of str2:  <class 'str'>
type of val1:  <class 'float'>
type of val2:  <class 'float'>
val1 =  10.23
val2 =  1001.0

Recommended posts

推荐的帖子

翻译自: https://www.includehelp.com/python/parse-a-string-to-float-in-python.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值