English conversion from number to string
Project description
shortscale
https://pypi.org/project/shortscale/
Python module to convert integers into English words.
This is the Python port of the shortscale function, originally written in JavaScript and Rust, documented here. There is a also a Go version.
The short scale, has different words for each power of 1000.
This implementation expresses positive and negative numbers from zero to thousands, millions, billions, trillions, quadrillions etc, up to 10**33 - 1.
Function
def shortscale(num: int) -> str
Example
import shortscale
# ==> four hundred and twenty billion nine hundred and ninety nine thousand and fifteen
print(shortscale.shortscale(420_000_999_015))
After installing this module, the function can also be called from the commnd line e.g.
$ shortscale 420_000_999_015
420,000,999,015 => four hundred and twenty billion nine hundred and ninety nine thousand and fifteen
$ shortscale 0xffffffff
4,294,967,295 => four billion two hundred and ninety four million nine hundred and sixty seven thousand two hundred and ninety five
Benchmarks
$ pip install -e .
Python v3.11.2 $ python tests/bench_shortscale.py
50000 calls, 5000000 bytes, 1264 ns/call
100000 calls, 10000000 bytes, 1216 ns/call
200000 calls, 20000000 bytes, 1216 ns/call
Python v3.10.9 $ python tests/bench_shortscale.py
50000 calls, 5000000 bytes, 1811 ns/call
100000 calls, 10000000 bytes, 1808 ns/call
200000 calls, 20000000 bytes, 1809 ns/call
Test
$ pip install pytest
$ pip install -e .
$ pytest
============================= test session starts ==============================
platform darwin -- Python 3.10.9, pytest-7.2.1, pluggy-1.0.0
rootdir: /Users/jldec/pub/shortscale-py
collected 1 item
tests/test_shortscale.py . [100%]
============================== 1 passed in 0.00s ===============================
Build
This assumes that access to pypi.org has been configured
pip install build twine
python -m build
python -m twine upload dist/*
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
shortscale-1.3.1.tar.gz
(5.8 kB
view hashes)
Built Distribution
Close
Hashes for shortscale-1.3.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 490f243a3dcf170d43ab6182af3f751e66a65e36cefdd6cacb277b98185fc9bb |
|
MD5 | 25b5acbe9e4767df1a857a9649afc069 |
|
BLAKE2b-256 | 148f0e219a14cba4d200833f9170e645181393a2aeea0b2b56d974d668ff152d |