Skip to main content

English conversion from number to string

Project description

shortscale

CI

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 tests/bench_shortscale.py 
         1 calls,        100 bytes,    11750 ns/call
         2 calls,        200 bytes,     5584 ns/call
         5 calls,        500 bytes,     4367 ns/call
        10 calls,       1000 bytes,     4158 ns/call
        20 calls,       2000 bytes,     4087 ns/call
        50 calls,       5000 bytes,     4043 ns/call
       100 calls,      10000 bytes,     4063 ns/call
       200 calls,      20000 bytes,     4055 ns/call
       500 calls,      50000 bytes,     3914 ns/call
      1000 calls,     100000 bytes,     3839 ns/call
      2000 calls,     200000 bytes,     3426 ns/call
      5000 calls,     500000 bytes,     3044 ns/call
     10000 calls,    1000000 bytes,     2479 ns/call
     20000 calls,    2000000 bytes,     2131 ns/call
     50000 calls,    5000000 bytes,     2067 ns/call
    100000 calls,   10000000 bytes,     2072 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


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.1.1.tar.gz (4.7 kB view hashes)

Uploaded Source

Built Distribution

shortscale-1.1.1-py3-none-any.whl (5.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page