As can be seen in documentation, to use thousands-square formatting using the character ,
, the following code is sufficient:
'{0:,}'.format(num_int)
If you want to modify the ,
for .
:
'{0:,}'.format(num_int).replace(',','.')
This only goes for the version of python 2.7+. From this point on response from the SOEN, it is possible to make older versions using the locale.format(), as follows:
import locale
locale.setlocale(locale.LC_ALL, '') #pega o local da máquina e seta o locale
locale.format('%d', num_int, 1)
This will take the location tab. In the case of Brazil, for example, will be the .
, in the case of some countries, such as the US, the ,
. Remember that it is possible to change the second parameter to the desired location.
Example with question code
Python 2.7+
Code:
valor_m = int(input("Digite um valor em metros: ")) #se for 2.x deve ser raw_input ao invés de input
valor_mm = valor_m * 1000
resultado = '{0:,}'.format(valor_mm).replace(',','.') #Aqui coloca os pontos
print (resultado)
Input and output:
input: 2345
output: 2.345.000
Python 2.6-
Code:
import locale
valor_m = int(raw_input("Digite um valor em metros: "))
valor_mm = valor_m * 1000
locale.setlocale(locale.LC_ALL, '') #pega o local da máquina e seta o locale
resultado = locale.format('%d', valor_mm, 1)
print resultado
Input and output:
input: 2345
output:
'Portuguese_Brazil.1252' #Caso esteja interpretador no modo interativo
2.345.000
I haven’t solved it yet, I just need to treat the result in value_mm to have a point every three digits, counting from right to left. How would this apply to the above code?
– Matt Costa