Python 定義了類型轉換函數來直接將一種數據類型轉換為另一種數據類型。本文旨在提供有關將十進製轉換為字符串的信息。
將十進製轉換為字符串
str() 方法可用於在 Python 中將十進製轉換為字符串。
用法:str(object, encoding=’utf-8?, errors=’strict’)
參數:
object:要返回其字符串表示的對象。
encoding:給定對象的編碼。
errors:解碼失敗時的響應。
範例1:
Python3
from decimal import Decimal
dec = Decimal(10)
print(dec, type(dec))
# Converting to string
dec = str(dec)
print(dec, type(dec))
輸出:
10 <class 'decimal.Decimal'> 10 <class 'str'>
範例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 <class 'decimal.Decimal'> 0.01 <class 'decimal.Decimal'>
相關用法
- Python Decimal轉Hexadecimal用法及代碼示例
- Python Decimal轉Other Bases用法及代碼示例
- Python Decimal compare()用法及代碼示例
- Python Decimal is_subnormal()用法及代碼示例
- Python Decimal logb()用法及代碼示例
- Python Decimal conjugate()用法及代碼示例
- Python Decimal copy_sign()用法及代碼示例
- Python Decimal compare_total()用法及代碼示例
- Python Decimal compare_total_mag()用法及代碼示例
- Python Decimal is_signed()用法及代碼示例
- Python Decimal is_normal()用法及代碼示例
- Python Decimal adjusted()用法及代碼示例
- Python Decimal as_integer_ratio()用法及代碼示例
- Python Decimal is_qnan()用法及代碼示例
- Python Decimal as_tuple()用法及代碼示例
- Python Decimal compare_signal()用法及代碼示例
- Python Decimal canonical()用法及代碼示例
注:本文由純淨天空篩選整理自deepanshumehra1410大神的英文原創作品 Convert Decimal to String in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。