博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python将字符串转换为浮点数
阅读量:2531 次
发布时间:2019-05-11

本文共 2420 字,大约阅读时间需要 8 分钟。

We can convert a string to float in Python using float() function. It’s a built-in function to convert an object to floating point number. Internally function calls specified object __float__() function.

我们可以使用float()函数将字符串转换为float在Python中。 它是将对象转换为浮点数的内置函数。 内部函数调用指定的对象__float __()函数。

Python将字符串转换为浮点数 (Python Convert String to float)

Let’s look at a simple example to convert a string to float in Python.

让我们看一个简单的示例,将字符串转换为浮点数(在Python中)。

s = '10.5674'f = float(s)print(type(f))print('Float Value =', f)

Output:

输出:

Float Value = 10.5674

为什么我们需要将字符串转换为float? (Why do we need to convert a string to float?)

If we are getting float value from user input through the terminal or reading it from a file, then they are string objects. So we have to explicitly convert them to float so that we can perform necessary operations on it, such as addition, multiplication etc.

如果我们通过终端从用户输入中获取浮点值或从文件中读取浮点值,则它们是字符串对象。 因此,我们必须将它们显式转换为float,以便对其执行必要的操作,例如加法,乘法等。

input_1 = input('Please enter first floating point value:\n')input_1 = float(input_1)input_2 = input('Please enter second floating point value:\n')input_2 = float(input_2)print(f'Sum of {input_1} and {input_2} is {input_1+input_2}')
block to catch exceptions in case of invalid input from user. 块来捕获异常。

If you are not familiar with string formatting using f prefix, please read .

如果您不熟悉使用f前缀的字符串格式,请阅读 。

Python将float转换为String (Python Convert float to String)

We can convert float to a string easily using str() function. This might be required sometimes where we want to concatenate float values. Let’s look at a simple example.

我们可以使用str()函数轻松地将float转换为字符串。 有时在我们要连接浮点值的地方可能需要这样做。 让我们看一个简单的例子。

f1 = 10.23f2 = 20.34f3 = 30.45# using f-string from Python 3.6+, change to format() for older versionsprint(f'Concatenation of {f1} and {f2} is {str(f1) + str(f2)}')print(f'CSV from {f1}, {f2} and {f3}:\n{str(f1)},{str(f2)},{str(f3)}')print(f'CSV from {f1}, {f2} and {f3}:\n{", ".join([str(f1),str(f2),str(f3)])}')

Output:

输出:

Concatenation of 10.23 and 20.34 is 10.2320.34CSV from 10.23, 20.34 and 30.45:10.23,20.34,30.45CSV from 10.23, 20.34 and 30.45:10.23, 20.34, 30.45

If we don’t convert float to string in the above program, will throw exception. Also, we won’t be able to use + operator to concatenate as it will add the floating point numbers.

如果在上述程序中未将float转换为字符串, 将引发异常。 另外,我们将无法使用+运算符进行连接,因为它将添加浮点数。

. 检出完整的python脚本和更多Python示例。

Reference:

参考:

翻译自:

转载地址:http://ummzd.baihongyu.com/

你可能感兴趣的文章
第四阶段 15_Linux tomcat安装与配置
查看>>
NAS 创建大文件
查看>>
学习笔记-模块之xml文件处理
查看>>
接口测试用例
查看>>
面试:用 Java 实现一个 Singleton 模式
查看>>
Sybase IQ导出文件的几种方式
查看>>
案例:手动输入一个字符串,打散放进一个列表,小写字母反序 大写字母保持不变...
查看>>
linux 系统下 tar 的压缩与解压缩命令
查看>>
阿里负载均衡,配置中间证书问题(在starcom申请免费DV ssl)
查看>>
转:How to force a wordbreaker to be used in Sharepoint Search
查看>>
MySQL存储过程定时任务
查看>>
Python中and(逻辑与)计算法则
查看>>
POJ 3267 The Cow Lexicon(动态规划)
查看>>
设计原理+设计模式
查看>>
音视频处理
查看>>
tomcat 7服务器跨域问题解决
查看>>
前台实现ajax 需注意的地方
查看>>
Jenkins安装配置
查看>>
个人工作总结05(第二阶段)
查看>>
Java clone() 浅拷贝 深拷贝
查看>>