0

Is there an existing library that would convert variable user input into an integer? User input can also have a magnitude suffix "K, M, G"

K-1000
M-1000000
G-1000000000

Input String -> Integer
"22.5" -> 22
"22.5K" -> 22500
"1,234" -> 1234
"1,234M" -> 1234000000
"0.1G" -> 100000000
"1.234E3" -> 1234000
Borisw37
  • 739
  • 2
  • 7
  • 30
  • For a simple version, maybe `int(string.replace('K', '000').replace('M', '000000').replace('G', '000000000'))`. Obviously this doesn’t satisfy all of the conditions. – N Chauhan Jun 27 '19 at 15:11
  • 1
    Give a try to `humanfriendly` package : https://pypi.org/project/humanfriendly/ For example if you do `num = humanfriendly.parse_size('16G')` then num should be 16000000000. Hope it helps. – Flo Jun 27 '19 at 15:12
  • for string of sort `"123e7"` (doesn't matter if it's capitol e) you can use `eval("123E7")` and get it as an integer – Rotem Tal Jun 27 '19 at 17:22

0 Answers0