numformat
Numformat is a library that helps you easily format number-like objects (int, float, decimal.Decimal, str) in a way that is consistent with scientific and engineering practices.
It has options to convert number-like objects to strings with different notations while allowing control over Significant Figures, Precision, and output string length.
Usage examples:
Importing the package:
import numformat as nf
Formatting to scientific and engineering notations:
# Convert to scientific notation
nf.sciformat(103000000)
>> '1.03e+08'
# Convert to engineering notation
nf.enggformat("103000000")
>> '103E+6'
Using autoformat:
# Auto-format with default options
nf.autoformat(1030000.0)
>> '1030000.0'
nf.autoformat(103000000.0)
>> '1.03e+08'
Using autoformat with the max_sig_figs argument:
# Auto-format and use a maximum of 5 significant figures
nf.autoformat(103000.000, max_sig_figs=5)
>> '103000'
# Auto-format and use a maximum of 2 significant figures
nf.autoformat("10300.00", max_sig_figs=2)
>> '10000'
Using autoformat with the max_length argument:
# Auto-format to a string that has a maximum length of 6 characters
nf.autoformat(103000000, max_length=6)
>> '1e+08'
Using autoformat with the preserve_sig_figs argument:
# Auto-format to a string that has a maximum length of 8 characters
nf.autoformat(103100000, max_length=8, preserve_sig_figs=False)
>> '1.03e+08'
# Auto-format to a string that has a maximum length of 8 characters but also preserve all significant figures.
nf.autoformat(103100000, max_length=8, preserve_sig_figs=True)
>> '103.1E+6'
Controlling the format type in autoformat:
# Auto-format to a string with a maximum length of 6 characters but don't allow scientific notation
nf.autoformat(103000000, max_length=6, allow_scientific_notation=False)
>> '103E+6'
# Auto-format to a string with a maximum length of 6 characters but don't allow scientific or engineering notation.
# Conditions cannot be met, therefore, it returns the number in standard notation.
nf.autoformat(103000000, max_length=6, allow_scientific_notation=False, allow_engineering_notation=False)
>> '103000000'
Raising an error if formatting fails:
# Auto-format to a string with a maximum length of 6 characters but don't allow scientific or engineering notation.
# Conditions cannot be met, and strict = True, therefore, returns an error.
nf.autoformat(103000000, max_length=6, allow_scientific_notation=False, allow_engineering_notation=False, strict=True)
>> ValueError: Could not represent the input within 6 characters.
Metadata
Release files for numformat 0.0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| numformat-0.0.4.tar.gz | 8.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| numformat-0.0.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 17.1 kB
Release files / numformat-0.0.4.tar.gz
| Download URL | numformat-0.0.4.tar.gz |
|---|---|
| Size | 8.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f55bbb0a0258ce978280f1bb34b6af0132634a1e1017abe2a8ae5ba640b39ccb
|
|
BLAKE2b-256 checksum How to use checksums |
32709fa199053acf3bbaa5b6ced8b7637745a90f66dcb3a81841614b013b0047
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/3.9.0 pkginfo/1.7.0 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.5
|
Release files / numformat-0.0.4-py3-none-any.whl
| Download URL | numformat-0.0.4-py3-none-any.whl |
|---|---|
| Size | 8.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
700f7969c847a50af9875609ce119997f0007a9b44e77442e20cb0a4dbe3bd98
|
|
BLAKE2b-256 checksum How to use checksums |
4a2b255dc4e355d683a5536c7f0502cfa5250ab4aec117f9fdc316636d63fe59
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/3.9.0 pkginfo/1.7.0 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.5
|