Convert number to million python
With Python2.7+ or v3 you just use the "," option in your string formatting - see PEP 378: Format Specifier for Thousands Separator for more info:
With Python3.6+ you can also use the "_" format - see PEP 515 for details:
or you can use a similar syntax with f-strings:
So I don't have to squint while reading big numbers, I have the 2.7 version of the code wrapped up in a shell function:
Lastly, if for some reason you must work with code from Python versions 2.6 or earlier, you can solve the problem using the View Discussion Improve Article Save Article View Discussion Improve Article Save Article Numerize is that library of python which is used to show large numbers into its readable format. It basically converts numerical
format into the compact short format. There is no need to show how many zeroes are behind the number. It itself checks the numeric digits behind the coefficient and then provides output in the compact short form accordingly. Key FeatureIt enhances the understanding as there is no need to write a number of zeroes behind the number coefficient(also called as trailing zeroes). 1 -> 1 10 -> 10 100 -> 100 1000 -> 1k 1500 -> 1.5k 1000000 -> 1M 1000000000 -> 1B 1000000000000 -> 1T Note:
Installing LibraryTo install this module type the below command in the terminal. pip install numerize Example 1: Python3
Output: 100 1k 1.5k 1M 1.12M 10B Example 2: Python3
Output: 1.23M 1.2479M 12.134K This is not a school project, just something to help me learn some Python! Just looking to see what I can improve on, looking for tips to reduce size & make it a bit easier for the computer to run it. |