0

I noticed a way to change an into to a comma separated number here. Neat trick!

I was wondering if there is a way using format to reverse this, as opposed to doing something like this:

import re
def comma_num_to_int(text):
  n = re.sub(",", "", text)
  return int(n)

my_big_number = "1,234,500"
print comma_num_to_int (my_big_number) # 1234500 
Ghoul Fool
  • 5,346
  • 9
  • 63
  • 111

1 Answers1

2

It is better to use locale module in python std library than replacing commas with replace.

>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'en_US.UTF8')
'en_US.UTF8'
>>> my_big_number = "1,234,500"
>>> print(locale.atoi(my_big_number))
1234500
>>> print(type(locale.atoi(my_big_number)))
<class 'int'>
All Іѕ Vаиітy
  • 22,178
  • 14
  • 79
  • 103