Python十进制转换为字符串 [英] Python Decimal to String
本文介绍了Python十进制转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这里有大量的主题,它们解释了如何将字符串转换为十进制,但是如何将十进制转换回字符串?
There are tons of topics on here that explain how to convert a string to a decimal, but how do I convert a decimal back to a string?
就像我是这样做的:
import decimal
dec = decimal.Decimal('10.0')
我要如何获取 dec
并获得' 10.0'
(字符串)输出?
How would I take dec
and get '10.0'
(a string) out?
推荐答案
使用内置的 str()
,其中:
Use the str()
builtin, which:
返回一个包含对象的可打印表示形式的字符串。
Returns a string containing a nicely printable representation of an object.
例如:
>>> import decimal
>>> dec = decimal.Decimal('10.0')
>>> str(dec)
'10.0'
这篇关于Python十进制转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文