Formatting of float types

Asked

Viewed 368 times

0

I did a little research before I asked, can you tell me why it doesn’t show 2,500 on the way out, but 2.5

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

print(salario)

1 answer

2

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

print(2.5 == 2.500)

Will return True

The point is used as separate from the "floating point", That is why it is called "floating point", then the right zeros are deleted, the same occurs with calculators.

Now if your goal is to use , as separator and points as thousand separators the story is another, 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 string passed 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)
  • In my case it was for the amount R $ 2.500,00.

  • @Andrésilva then uses the second example I posted, understand one thing, number is number, string is string, that for the computer is not a number "2,500.00" is a string, with the replace as shown you convert to number.

  • Got it man thanks a lot for the tips, I’ll try!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.