Cannot say when the function is introduced, or if it even is. However, it is a simple function to learn and use, as the documentation will show.
Essentially, rounding follows one simple rule:
up on 5
down on 4
Let’s look at PI as an example:
3.1415926...
Above there are seven decimal places showing. To reduce it to six, we round up on 6 (3.141593). To reduce to five, we round down on 2 (3.14159). For four, up on 9 (3.1416), for three, up on 5 (3.142). For two decimal places we round down on 1 (3.14).
This is the exact behavior of the round()
function.
Rounding does not remove the decimal point, though.
round(pi, 0)
3.0
However, 3.0 is equal to 3 in terms of value and magnitude. That’s as close as round()
can bring us.
Truth is, mathematically there is no way to tell if a number is truly an integer, since that is a type. Allowing for the decimal point zero has to be a given in solving this problem.
We can also use the modulo rule,
n % 1 is zero, if n is an integer (or the decimal fraction is zero)
When the result is non-zero, then there must be some decimal fraction.
print (pi % 1)
0.14159265358979312