Formatting float types

0

I researched a little before asking, they know why it does not show 2,500 in the output, but 2.5

salario=float(input("Digite seu salario"))

print(salario)
    
asked by anonymous 01.05.2018 / 04:36

1 answer

2

Because 2.5 equals 2,500 , just do the test:

print(2.5 == 2.500)

Will return True

The point is used as separate from the "floating point" , so this is called "floating point" , then the right zeros are suppressed, so does calculators.

Now if your goal is to use , as a separator and points like thousands separators the story is another, you would have to use replace , for example:

x = '2.500,01'
x = x.replace('.', '').replace(',', '.')
x = float(x)
print(x)

If you have R$ in the last string use:

x = 'R$ 2.500,01'

x = x.replace('R$', '') # remove o R$
x = x.strip()           # remove espaços em branco
x = x.replace('.', '')  # remove os pontos
x = x.replace(',', '.') # troca  virgula do decimal por ponto

x = float(x)
print(x)
    
01.05.2018 / 04:44