0

I have this simple code:

val = float(input('insira o salario:  '))

print('o novo salario e {} reais'.format(val * 1.15))

When I insert 1000, it prints 1150. But when I insert 100, it prints literally 114.99999999999999.

I am really new into programming and I couldn't find an answer anywhere. Why is this happening?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Pedro Henrique
  • 181
  • 3
  • 11
  • 4
    Does this answer your question? [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Keldorn May 05 '20 at 02:27
  • 2
    Welcome to programming! This looks like it might be a 'floating point error'. Under the hood, python (and most languages) store decimal numbers a little differently than you might think. You might try googling 'floating point arithmetic'. – xdhmoore May 05 '20 at 02:27
  • 2
    This has to do with floating point precision. Read https://docs.python.org/3/tutorial/floatingpoint.html for more information. – jkr May 05 '20 at 02:35
  • 1
    Sometimes when storing floating point numbers you can end up with small rounding errors. You can try multiplying by 115 and then dividing by 100. Since it appears that you are working with monetary values you can also use the round function to round your answer to 2 decimal places ie. print('o novo salario e {} reais'.format(round((val * 115) / 100), 2) – Lateralus May 05 '20 at 02:47
  • Thank you all for de clarification, it helped a lot. Idk how to upvote yet, but I'm really thankful. – Pedro Henrique May 05 '20 at 15:46

0 Answers0