在Python中将十进制转换为字符串
Python定义了类型转换函数来直接将一种数据类型转换为另一种数据类型。本文旨在提供有关将小数转换为字符串的信息。
将十进制转换为字符串
str() 方法可用于在Python中将十进制转换为字符串。
Syntax: str(object, encoding=’utf-8?, errors=’strict’)
Parameters:
object: The object whose string representation is to be returned.
encoding: Encoding of the given object.
errors: Response when decoding fails.
示例 1:
Python3
from decimal import Decimal
dec = Decimal(10)
print(dec, type(dec))
# Converting to string
dec = str(dec)
print(dec, type(dec))
Python3
from decimal import Decimal
dec = Decimal("0.01")
print(dec, type(dec))
# Converting decimal to string
s = str(dec)
print(s, type(dec))
输出:
10
10
示例 2:
Python3
from decimal import Decimal
dec = Decimal("0.01")
print(dec, type(dec))
# Converting decimal to string
s = str(dec)
print(s, type(dec))
输出:
0.01
0.01