Python中实现float转为String
发布时间:2025-11-20 11:18:26

之前介绍过Python中float() 函数的实现过程(https://www.py.cn/jishu/jichu/21933.html)。使用float() 在函数中,我们使用的字符是浮点数,但有时我们不需要浮点数,然后我们将float转换为其他形式。本文将介绍它Python float 转换为 String有两种方法:使用'%d'%实现num和使用str()方法。
方法一:使用'%d'%num实现
num=322 str1='%d'%num#转为整型 print(str1) >>> '32' num=32.348 str2='%f'%32.348#转为浮点型 print(str2) >>> '32.348000'
方法二:采用str()方法实现
pi=3.1415 print(type(pi))#float piInString=str(pi)#float->str print(type(piInString))#str
输出
<class'float'> <class'str'>
以上是Python中将floattat 转换为 String的两种方法都是非常简单的方法。你可以选择你喜欢的方法进行转换~
下一篇 返回列表
