M
M
Max Payne2017-04-01 21:30:58
Python
Max Payne, 2017-04-01 21:30:58

How to correctly implement the reduction of large numbers?

Recently it was necessary to write a system for reducing numbers, that is, 1.000 - 1K, 2.000.000 - 2M. I came up with a simple solution for myself:

def shortint(textint):
        textint = str(textint)
        m = {0: '', 3: 'K', 6: 'M', 9: 'B', 12: 'T'}
        k = (len(textint)-1)//3*3
        if k == 0:
            return textint
        else:
            return textint[:-k] + m[k]

I understand that the solution is not entirely correct.
However, now it took a reduction to tenths, that is, 1.000 - 1.0K, 123.456 - 123.5K.
Hint where to look to implement it correctly :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2017-04-01
@sim3x

hint
use the decimal logarithm

A bunch of solutions from libraries to custom
stackoverflow.com/questions/10969759/python-librar...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question