Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách nhân tính (humanize) số trong Python


Nếu muốn chuyển số thành từ như 99 thành ninety-nine, phải dùng gói ngoài hoặc tự viết. Module pynum2word khá tốt. Cài bằng −

$ pip install pynum2word

Dùng như sau −

>>> import num2word >>> num2word.to_card(16) 'sixteen' >>> num2word.to_card(23) 'twenty-three' >>> num2word.to_card(1223)

'one thousand, two hundred and twenty-three'

Nếu muốn kết quả như 1.23 million cho 1,230,000, dùng thư viện humanize. Cài bằng −

$ pip install humanize

Dùng như sau −

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