Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธี Humanize ตัวเลขด้วย Python (แปลงเป็นคำอ่าน)

ถ้าต้องการแปลงจำนวนเต็มเป็นคำอ่าน เช่น 99 เป็น ninety-nine ต้องใช้แพ็กเกจภายนอกหรือสร้างเอง pynum2word ทำได้ดี ติดตั้งด้วย

$ pip install pynum2word

ใช้ดังนี้

>>> import num2word
>>> num2word.to_card(16)
'sixteen'
>>> num2word.to_card(23)
'twenty-three'
>>> num2word.to_card(1223)
'one thousand, two hundred and twenty-three'

ถ้าต้องการผลลัพธ์แบบ 1.23 million สำหรับ 1,230,000 ใช้ humanize library ติดตั้งด้วย

$ pip install humanize

ใช้ดังนี้

>>> import humanize
>>> humanize.intword(1230000)
'1.23 million'