Round question in python (decimal)

Asked 2 years ago, Updated 2 years ago, 28 views

round(1.5)
round(2.5)
round(3.5)
round(4.5)
round(5.5)
round(6.5)

The results of the respective

2
2
4
4
6
6

It's coming out. I thought it was a function of rounding off I wonder why the results are coming out like this. Shouldn't 2, 3, 4, 5, 6 7 come out?

Note> Run your answers at http://tryhelloworld.co.kr/questions/1087 and move them to your own answers.

python round

2022-09-22 16:07

1 Answers

Looking at the Python document, it says .

Note The behavior of round() for floats can be surprising: for example, round(2.675, 2) gives 2.67 instead of the expected 2.68. This is not a bug: it’s a result of the fact that most decimal fractions can’t be represented exactly as a float. See Floating Point Arithmetic: Issues and Limitations for more information.

If you interpret it, you round 2.675 to the second decimal place and you get 2.67. This is a problem that arises because it is difficult to accurately represent decimal numbers. It's not a bug.

That's what it says.


2022-09-22 16:07

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.