Convert long numbers into a human-readable format in Python

Overview

millify

Convert long numbers into a human-readable format in Python

Installation

pip install millify

Usage

Convert to number with binary prefix

from millify import millify

millify(1234)
# '1k'

millify('1234') # same for strings
# '1k'

millify(12345678)
# '12M'

millify(12345678, precision=2)
# '12.35M'

millify(10000, precision=2) # hide nulls in decimals by default
# '10k'

millify(10000, precision=2, drop_nulls=False)
# '10.00k'

prefixes = ['kB', 'MB', 'GB']
millify(10000, prefixes=prefixes)
# '10kB'

Add a thousands separator

from millify import prettify

prettify(1234)
# '1,234'

prettify('1234') # same for strings
# '1,234'

prettify(1234, '`')
# '1`234'
Comments
  • Format to a fixed number of characters would be a nice addition

    Format to a fixed number of characters would be a nice addition

    Sometimes you want a fixed number of characters to be used for a number (for logging for example).

    With 5 characters (this includes numbers, decimal points and magnitude):

    4000000 => 4.00M
    40000   => 40.0K
    4000    => 4.00K
    400     => 0.40K (400 and 400.0 are wrong as they take 3 and 5 places)
    40      => 0.04K or 40.00
    4       => 4.000
    
    opened by ddofborg 0
  • Code error on prettify function

    Code error on prettify function

    The function prettify has a self-loop, but it doesn't account for a different character in the loop it just affects the first iteration. This is the solution to the error.

    def prettify(amount, separator=','): """Separate with predefined separator.""" orig = str(amount) new = re.sub("^(-?\d+)(\d{3})", "\g<1>{0}\g<2>".format(separator), str(amount)) if orig == new: return new else: return prettify(new, separator)

    opened by RobertTura 0
  • fix prettify custom separator

    fix prettify custom separator

    prettify was only using the custom separator once, then all latter separators defaulted to comma. reproducible with long numbers, e.g. prettify(1000000000, "-") -> '1,000,000-000'

    opened by 0xDECAFFED 0
  • Fix

    Fix "Nines" bug and add tests

    The code in this PR is build on top of @weefatboi's work (#2). tests\test_millify.py now contains unit tests that test the fix by @weefatboi, and the existing code. Based on the tests, three changes are made to __init__.py:

    • Only use custom prefixes if a list is passed;
    • Only use a custom separator if a string is passed;
    • For numbers with more than five digits that were passed to prettify() with a custom separator, the custom separator only appeared as last thousand separator. This is fixed by passing separator on the recursive call of prettify().

    After these changes, all tests were passed.

    opened by MitchAngenent 1
  • "Nines" bug fix

    What bug does this PR fix?

    • There's a bug where iterations of three 9s (999, 999999, 999999999, etc) should round up to the next millname (999999 should become 1M, but displays 1000k).
    • The cause was usage of the math.floor() when determining which prefix to append to inputted numbers. Floor rounds down to the nearest whole integer, forcing numbers that should be rounded up have one less millidx than they should at the time of string formatting. This has been replaced with python's native round() function, and has maintained the ability to use precision as an input parameter.
    • drop_nulls logic has also been preserved in such a way that it happens during string formatting, thus the removal of remove_exponents() function which is unneeded with the current changes.
    • prettify() logic remains untouched

    How was this PR tested?

    • Ran series of millify() calls on numbers with known bugged outputs, and verified they now round up as intended.
    • Ran series of millify() calls on non-bugged numbers to verify they still work as intended, toggling the various outputs and testing those as well
    • NOTE: using a precision= value of 3 or higher still produces 1000.000k instead of 1.000M. However, it's in my personal opinion that, when using this module, users shouldn't need/want more than two decimal places showing in their string output anyway. If these changes are accepted, this note should probably go in the README / patch notes. Further, if anyone can figure out how to preserve the correct millname while using a precision value of 3 or higher, I haven't been able to yet but am open to all ideas!

    Related Ticket

    • Closes #1
    • NOTE: not sure how versioning works for this project, but I've left version = 0.1.1 as-is in this PR. Should these changes be accepted, you can determine if this needed versioned up to 0.1.2 or left alone! Thanks for reading, and kind regards!

    Screenshots

    • before (with bug): Screen Shot 2021-06-17 at 1 49 20 PM
    • after (bug fixed): Screen Shot 2021-06-17 at 1 51 17 PM
    opened by weefatboi 4
Owner
Alex Zaitsev
Alex Zaitsev
Neogex is a human readable parser standard, being implemented in Python

Neogex (New Expressions) Parsing Standard Much like Regex, Neogex allows for string parsing and validation based on a set of requirements. Unlike Rege

Seamus Donnellan 1 Dec 17, 2021
This is a Python 3.10 port of mock, a library for manipulating human-readable message strings.

This is a Python 3.10 port of mock, a library for manipulating human-readable message strings.

Alexander Bartolomey 1 Dec 31, 2021
A command line interface tool converting starknet warp transpiled outputs into readable cairo contracts.

warp-to-cairo warp-to-cairo is a simple tool converting starknet warp outputs (NethermindEth/warp) outputs into readable cairo contracts. The warp out

Michael K 5 Jun 10, 2022
Process RunGap output file of a workout and load data into Apple Numbers Spreadsheet and my website with API calls

BSD 3-Clause License Copyright (c) 2020, Mike Bromberek All rights reserved. ProcessWorkout Exercise data is exported in JSON format to iCloud using

Mike Bromberek 1 Jan 3, 2022
Minimal, super readable string pattern matching for python.

simplematch Minimal, super readable string pattern matching for python. import simplematch simplematch.match("He* {planet}!", "Hello World!") >>> {"p

Thomas Feldmann 147 Dec 1, 2022
This code makes the logs provided by Fiddler proxy of the Google Analytics events coming from iOS more readable.

GA-beautifier-iOS This code makes the logs provided by Fiddler proxy of the Google Analytics events coming from iOS more readable. To run it, create a

Rafael Machado 3 Feb 2, 2022
A python script for combining multiple native SU2 format meshes into one mesh file for multi-zone simulations.

A python script for combining multiple native SU2 format meshes into one mesh file for multi-zone simulations.

MKursatUzuner 1 Jan 20, 2022
A simple python script to convert Rubber Ducky payloads into AutoHotKey scripts

AHKDuckyReplacer A simple python script to convert Rubber Ducky payloads into AutoHotKey scripts. I have also added a sample payload for testing. I wi

Krizsan0596 5 Sep 28, 2022
A Python script to convert your favorite TV series into an Anki deck.

Ankiniser A Python3.8 script to convert your favorite TV series into an Anki deck. How to install? Download the script with git or download it manualy

null 37 Nov 3, 2022
Simple utlity for sniffing decrypted HTTP/HTTPS traffic on a jailbroken iOS device into an HAR format.

Description iOS devices contain a hidden feature for sniffing decrypted HTTP/HTTPS traffic from all processes using the CFNetwork framework into an HA

null 83 Dec 25, 2022
Time tracking program that will format output to be easily put into Gitlab

time_tracker Time tracking program that will format output to be easily put into Gitlab. Feel free to branch and use it yourself! Getting Started Clon

Jake Strasler 2 Oct 13, 2022
Buildium-to-stessa - Automation to assist in converting Buildium transactions into Stessa format

Buildium Transactions -> Stessa Transactions There is currently no third-party i

Austin Comstock 4 Apr 17, 2022
Scripts to convert the Ted-MDB corpora into the formats for DISRPT shared task and the converted corpora

Scripts to convert the Ted-MDB corpora into the formats for DISRPT shared task and the converted corpora.

null 1 Feb 8, 2022
A practice program to find the LCM i.e Lowest Common Multiplication of two numbers using python without library.

Finding-LCM-using-python-from-scratch Here, I write a practice program to find the LCM i.e Lowest Common Multiplication of two numbers using python wi

Sachin Vinayak Dabhade 4 Sep 24, 2021
A check numbers python module

Made with Python3 (C) @FayasNoushad Copyright permission under MIT License License -> https://github.com/FayasNoushad/Numbers/blob/main/LICENSE Deplo

Fayas Noushad 3 Nov 28, 2021
A numbers check python package

A numbers check python package

Fayas Noushad 3 Nov 28, 2021
A numbers extract from string python package

Made with Python3 (C) @FayasNoushad Copyright permission under MIT License License -> https://github.com/FayasNoushad/Numbers-Extract/blob/main/LICENS

Fayas Noushad 4 Nov 28, 2021
Validate UC alumni identifier numbers with Python 3.

UC number validator Validate UC alumni identifier numbers with Python 3. Getting started Install the library with: pip install -U ucnumber Usage from

Open Source eUC 1 Jul 7, 2021
A python library what works with numbers.

pynum A python library what works with numbers. Prime Prime class have everithing you want about prime numbers. check_prime The check_prime method is

Mohammad Mahdi Paydar Puya 1 Jan 7, 2022