round() returns different result depending on the number of arguments
Clash Royale CLAN TAG#URR8PPP
up vote
20
down vote
favorite
While using the round()
function I noticed that I get two different results depending on whether I don't explicitly choose the number of decimal places to include or choosing the number to be 0.
x = 4.1
print(round(x))
print(round(x, 0))
It prints the following:
4
4.0
What is the difference?
python python-3.x rounding
New contributor
add a comment |Â
up vote
20
down vote
favorite
While using the round()
function I noticed that I get two different results depending on whether I don't explicitly choose the number of decimal places to include or choosing the number to be 0.
x = 4.1
print(round(x))
print(round(x, 0))
It prints the following:
4
4.0
What is the difference?
python python-3.x rounding
New contributor
1
I get 4.0 in both cases (python 3.5)
â taras
8 hours ago
5
+1 I was surprised by the downvote. This is a good question, with runnable code, probing a counterintuitive edge-case. I certainly learned something from this. Thanks, Peter.
â Bill Cheatham
8 hours ago
6
@taras: nope,round(4.1)
in Python 3.5 produces4
, onlyround(4.1, 0)
produces4.0
. Do triple-check your Python versions. Useimport sys; print(sys.version_info)
from inside Python if you have to, because the behaviour you are reporting is specific to Python two. Theround()
function documentation for Python 3 covers this case explicitly: Ifndigits
is omitted or isNone
, it returns the nearest integer to its input..
â Martijn Pietersâ¦
8 hours ago
1
@MartijnPieters, thank you for pointing it out. Apparently, I've checked it using 2.7 thinking I was running 3.5.
â taras
7 hours ago
How is this not a duplicate after more than 10 years and more than 15 million questions?
â Peter Mortensen
2 hours ago
add a comment |Â
up vote
20
down vote
favorite
up vote
20
down vote
favorite
While using the round()
function I noticed that I get two different results depending on whether I don't explicitly choose the number of decimal places to include or choosing the number to be 0.
x = 4.1
print(round(x))
print(round(x, 0))
It prints the following:
4
4.0
What is the difference?
python python-3.x rounding
New contributor
While using the round()
function I noticed that I get two different results depending on whether I don't explicitly choose the number of decimal places to include or choosing the number to be 0.
x = 4.1
print(round(x))
print(round(x, 0))
It prints the following:
4
4.0
What is the difference?
python python-3.x rounding
python python-3.x rounding
New contributor
New contributor
edited 12 mins ago
Aran-Fey
20.3k53166
20.3k53166
New contributor
asked 8 hours ago
Peter Hofer
1144
1144
New contributor
New contributor
1
I get 4.0 in both cases (python 3.5)
â taras
8 hours ago
5
+1 I was surprised by the downvote. This is a good question, with runnable code, probing a counterintuitive edge-case. I certainly learned something from this. Thanks, Peter.
â Bill Cheatham
8 hours ago
6
@taras: nope,round(4.1)
in Python 3.5 produces4
, onlyround(4.1, 0)
produces4.0
. Do triple-check your Python versions. Useimport sys; print(sys.version_info)
from inside Python if you have to, because the behaviour you are reporting is specific to Python two. Theround()
function documentation for Python 3 covers this case explicitly: Ifndigits
is omitted or isNone
, it returns the nearest integer to its input..
â Martijn Pietersâ¦
8 hours ago
1
@MartijnPieters, thank you for pointing it out. Apparently, I've checked it using 2.7 thinking I was running 3.5.
â taras
7 hours ago
How is this not a duplicate after more than 10 years and more than 15 million questions?
â Peter Mortensen
2 hours ago
add a comment |Â
1
I get 4.0 in both cases (python 3.5)
â taras
8 hours ago
5
+1 I was surprised by the downvote. This is a good question, with runnable code, probing a counterintuitive edge-case. I certainly learned something from this. Thanks, Peter.
â Bill Cheatham
8 hours ago
6
@taras: nope,round(4.1)
in Python 3.5 produces4
, onlyround(4.1, 0)
produces4.0
. Do triple-check your Python versions. Useimport sys; print(sys.version_info)
from inside Python if you have to, because the behaviour you are reporting is specific to Python two. Theround()
function documentation for Python 3 covers this case explicitly: Ifndigits
is omitted or isNone
, it returns the nearest integer to its input..
â Martijn Pietersâ¦
8 hours ago
1
@MartijnPieters, thank you for pointing it out. Apparently, I've checked it using 2.7 thinking I was running 3.5.
â taras
7 hours ago
How is this not a duplicate after more than 10 years and more than 15 million questions?
â Peter Mortensen
2 hours ago
1
1
I get 4.0 in both cases (python 3.5)
â taras
8 hours ago
I get 4.0 in both cases (python 3.5)
â taras
8 hours ago
5
5
+1 I was surprised by the downvote. This is a good question, with runnable code, probing a counterintuitive edge-case. I certainly learned something from this. Thanks, Peter.
â Bill Cheatham
8 hours ago
+1 I was surprised by the downvote. This is a good question, with runnable code, probing a counterintuitive edge-case. I certainly learned something from this. Thanks, Peter.
â Bill Cheatham
8 hours ago
6
6
@taras: nope,
round(4.1)
in Python 3.5 produces 4
, only round(4.1, 0)
produces 4.0
. Do triple-check your Python versions. Use import sys; print(sys.version_info)
from inside Python if you have to, because the behaviour you are reporting is specific to Python two. The round()
function documentation for Python 3 covers this case explicitly: If ndigits
is omitted or is None
, it returns the nearest integer to its input..â Martijn Pietersâ¦
8 hours ago
@taras: nope,
round(4.1)
in Python 3.5 produces 4
, only round(4.1, 0)
produces 4.0
. Do triple-check your Python versions. Use import sys; print(sys.version_info)
from inside Python if you have to, because the behaviour you are reporting is specific to Python two. The round()
function documentation for Python 3 covers this case explicitly: If ndigits
is omitted or is None
, it returns the nearest integer to its input..â Martijn Pietersâ¦
8 hours ago
1
1
@MartijnPieters, thank you for pointing it out. Apparently, I've checked it using 2.7 thinking I was running 3.5.
â taras
7 hours ago
@MartijnPieters, thank you for pointing it out. Apparently, I've checked it using 2.7 thinking I was running 3.5.
â taras
7 hours ago
How is this not a duplicate after more than 10 years and more than 15 million questions?
â Peter Mortensen
2 hours ago
How is this not a duplicate after more than 10 years and more than 15 million questions?
â Peter Mortensen
2 hours ago
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
19
down vote
accepted
The round function returns an integer if the second argument is not specified, else the return value has the same type as that of the first argument:
>>> help(round)
Help on built-in function round in module builtins:
round(number, ndigits=None)
Round a number to a given precision in decimal digits.
The return value is an integer if ndigits is omitted or None. Otherwise
the return value has the same type as the number. ndigits may be negative.
So if the arguments passed are an integer and a zero, the return value will be an integer type:
>>> round(100, 0)
100
>>> round(100, 1)
100
For the sake of completeness:
Negative numbers are used for rounding before the decimal place
>>> round(124638, -2)
124600
>>> round(15432.346, -2)
15400.0
I had no idea you could use negative numbers onround()
. Good to know!
â Daffy
43 mins ago
Huh, really the most important part of the answer here is theHelp
function, because that would allow anyone to get the answer and so much more.
â Nicholas Pipitone
30 mins ago
add a comment |Â
up vote
11
down vote
When you specify the number of decimals, even if that number is 0, you are calling the version of the method that returns a float. So it is normal that you get that result.
3
Yep, pretty straightforward. Just like it says inhelp(round)
, it "returns an int when called with one argument, otherwise the same type as the number."
â Kevin
8 hours ago
3
Might want to link to the docs and quote the first sentence.
â kabanus
8 hours ago
1
"version of the method that returns a float" no overloading or return types around here, it's just anif
statement checking forargs
â cat
6 hours ago
add a comment |Â
up vote
1
down vote
The round() function in Python takes two parameters:
- number - number to be rounded
- number of digits (optional) - the number of digits up to which the given number is to be rounded.
Whenever you use the second parameter, Python automatically converts the data type of the return value to float. If you don't use the second optional parameter then the data type remains an integer.
Therefore, it is 4.0 when the parameter is passed and 4 when it isn't.
This is incorrect. "Whenever you use the second parameter, Python automatically converts the data type of the return value to float.". Please cite sources. In this case, attempting to cite sources will show that the return type behaves as Aniket describes (And cites withHelp(round)
)
â Nicholas Pipitone
31 mins ago
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
19
down vote
accepted
The round function returns an integer if the second argument is not specified, else the return value has the same type as that of the first argument:
>>> help(round)
Help on built-in function round in module builtins:
round(number, ndigits=None)
Round a number to a given precision in decimal digits.
The return value is an integer if ndigits is omitted or None. Otherwise
the return value has the same type as the number. ndigits may be negative.
So if the arguments passed are an integer and a zero, the return value will be an integer type:
>>> round(100, 0)
100
>>> round(100, 1)
100
For the sake of completeness:
Negative numbers are used for rounding before the decimal place
>>> round(124638, -2)
124600
>>> round(15432.346, -2)
15400.0
I had no idea you could use negative numbers onround()
. Good to know!
â Daffy
43 mins ago
Huh, really the most important part of the answer here is theHelp
function, because that would allow anyone to get the answer and so much more.
â Nicholas Pipitone
30 mins ago
add a comment |Â
up vote
19
down vote
accepted
The round function returns an integer if the second argument is not specified, else the return value has the same type as that of the first argument:
>>> help(round)
Help on built-in function round in module builtins:
round(number, ndigits=None)
Round a number to a given precision in decimal digits.
The return value is an integer if ndigits is omitted or None. Otherwise
the return value has the same type as the number. ndigits may be negative.
So if the arguments passed are an integer and a zero, the return value will be an integer type:
>>> round(100, 0)
100
>>> round(100, 1)
100
For the sake of completeness:
Negative numbers are used for rounding before the decimal place
>>> round(124638, -2)
124600
>>> round(15432.346, -2)
15400.0
I had no idea you could use negative numbers onround()
. Good to know!
â Daffy
43 mins ago
Huh, really the most important part of the answer here is theHelp
function, because that would allow anyone to get the answer and so much more.
â Nicholas Pipitone
30 mins ago
add a comment |Â
up vote
19
down vote
accepted
up vote
19
down vote
accepted
The round function returns an integer if the second argument is not specified, else the return value has the same type as that of the first argument:
>>> help(round)
Help on built-in function round in module builtins:
round(number, ndigits=None)
Round a number to a given precision in decimal digits.
The return value is an integer if ndigits is omitted or None. Otherwise
the return value has the same type as the number. ndigits may be negative.
So if the arguments passed are an integer and a zero, the return value will be an integer type:
>>> round(100, 0)
100
>>> round(100, 1)
100
For the sake of completeness:
Negative numbers are used for rounding before the decimal place
>>> round(124638, -2)
124600
>>> round(15432.346, -2)
15400.0
The round function returns an integer if the second argument is not specified, else the return value has the same type as that of the first argument:
>>> help(round)
Help on built-in function round in module builtins:
round(number, ndigits=None)
Round a number to a given precision in decimal digits.
The return value is an integer if ndigits is omitted or None. Otherwise
the return value has the same type as the number. ndigits may be negative.
So if the arguments passed are an integer and a zero, the return value will be an integer type:
>>> round(100, 0)
100
>>> round(100, 1)
100
For the sake of completeness:
Negative numbers are used for rounding before the decimal place
>>> round(124638, -2)
124600
>>> round(15432.346, -2)
15400.0
edited 2 hours ago
Peter Mortensen
13.1k1983111
13.1k1983111
answered 7 hours ago
Aniket Navlur
449211
449211
I had no idea you could use negative numbers onround()
. Good to know!
â Daffy
43 mins ago
Huh, really the most important part of the answer here is theHelp
function, because that would allow anyone to get the answer and so much more.
â Nicholas Pipitone
30 mins ago
add a comment |Â
I had no idea you could use negative numbers onround()
. Good to know!
â Daffy
43 mins ago
Huh, really the most important part of the answer here is theHelp
function, because that would allow anyone to get the answer and so much more.
â Nicholas Pipitone
30 mins ago
I had no idea you could use negative numbers on
round()
. Good to know!â Daffy
43 mins ago
I had no idea you could use negative numbers on
round()
. Good to know!â Daffy
43 mins ago
Huh, really the most important part of the answer here is the
Help
function, because that would allow anyone to get the answer and so much more.â Nicholas Pipitone
30 mins ago
Huh, really the most important part of the answer here is the
Help
function, because that would allow anyone to get the answer and so much more.â Nicholas Pipitone
30 mins ago
add a comment |Â
up vote
11
down vote
When you specify the number of decimals, even if that number is 0, you are calling the version of the method that returns a float. So it is normal that you get that result.
3
Yep, pretty straightforward. Just like it says inhelp(round)
, it "returns an int when called with one argument, otherwise the same type as the number."
â Kevin
8 hours ago
3
Might want to link to the docs and quote the first sentence.
â kabanus
8 hours ago
1
"version of the method that returns a float" no overloading or return types around here, it's just anif
statement checking forargs
â cat
6 hours ago
add a comment |Â
up vote
11
down vote
When you specify the number of decimals, even if that number is 0, you are calling the version of the method that returns a float. So it is normal that you get that result.
3
Yep, pretty straightforward. Just like it says inhelp(round)
, it "returns an int when called with one argument, otherwise the same type as the number."
â Kevin
8 hours ago
3
Might want to link to the docs and quote the first sentence.
â kabanus
8 hours ago
1
"version of the method that returns a float" no overloading or return types around here, it's just anif
statement checking forargs
â cat
6 hours ago
add a comment |Â
up vote
11
down vote
up vote
11
down vote
When you specify the number of decimals, even if that number is 0, you are calling the version of the method that returns a float. So it is normal that you get that result.
When you specify the number of decimals, even if that number is 0, you are calling the version of the method that returns a float. So it is normal that you get that result.
answered 8 hours ago
Osuman AAA
295111
295111
3
Yep, pretty straightforward. Just like it says inhelp(round)
, it "returns an int when called with one argument, otherwise the same type as the number."
â Kevin
8 hours ago
3
Might want to link to the docs and quote the first sentence.
â kabanus
8 hours ago
1
"version of the method that returns a float" no overloading or return types around here, it's just anif
statement checking forargs
â cat
6 hours ago
add a comment |Â
3
Yep, pretty straightforward. Just like it says inhelp(round)
, it "returns an int when called with one argument, otherwise the same type as the number."
â Kevin
8 hours ago
3
Might want to link to the docs and quote the first sentence.
â kabanus
8 hours ago
1
"version of the method that returns a float" no overloading or return types around here, it's just anif
statement checking forargs
â cat
6 hours ago
3
3
Yep, pretty straightforward. Just like it says in
help(round)
, it "returns an int when called with one argument, otherwise the same type as the number."â Kevin
8 hours ago
Yep, pretty straightforward. Just like it says in
help(round)
, it "returns an int when called with one argument, otherwise the same type as the number."â Kevin
8 hours ago
3
3
Might want to link to the docs and quote the first sentence.
â kabanus
8 hours ago
Might want to link to the docs and quote the first sentence.
â kabanus
8 hours ago
1
1
"version of the method that returns a float" no overloading or return types around here, it's just an
if
statement checking for args
â cat
6 hours ago
"version of the method that returns a float" no overloading or return types around here, it's just an
if
statement checking for args
â cat
6 hours ago
add a comment |Â
up vote
1
down vote
The round() function in Python takes two parameters:
- number - number to be rounded
- number of digits (optional) - the number of digits up to which the given number is to be rounded.
Whenever you use the second parameter, Python automatically converts the data type of the return value to float. If you don't use the second optional parameter then the data type remains an integer.
Therefore, it is 4.0 when the parameter is passed and 4 when it isn't.
This is incorrect. "Whenever you use the second parameter, Python automatically converts the data type of the return value to float.". Please cite sources. In this case, attempting to cite sources will show that the return type behaves as Aniket describes (And cites withHelp(round)
)
â Nicholas Pipitone
31 mins ago
add a comment |Â
up vote
1
down vote
The round() function in Python takes two parameters:
- number - number to be rounded
- number of digits (optional) - the number of digits up to which the given number is to be rounded.
Whenever you use the second parameter, Python automatically converts the data type of the return value to float. If you don't use the second optional parameter then the data type remains an integer.
Therefore, it is 4.0 when the parameter is passed and 4 when it isn't.
This is incorrect. "Whenever you use the second parameter, Python automatically converts the data type of the return value to float.". Please cite sources. In this case, attempting to cite sources will show that the return type behaves as Aniket describes (And cites withHelp(round)
)
â Nicholas Pipitone
31 mins ago
add a comment |Â
up vote
1
down vote
up vote
1
down vote
The round() function in Python takes two parameters:
- number - number to be rounded
- number of digits (optional) - the number of digits up to which the given number is to be rounded.
Whenever you use the second parameter, Python automatically converts the data type of the return value to float. If you don't use the second optional parameter then the data type remains an integer.
Therefore, it is 4.0 when the parameter is passed and 4 when it isn't.
The round() function in Python takes two parameters:
- number - number to be rounded
- number of digits (optional) - the number of digits up to which the given number is to be rounded.
Whenever you use the second parameter, Python automatically converts the data type of the return value to float. If you don't use the second optional parameter then the data type remains an integer.
Therefore, it is 4.0 when the parameter is passed and 4 when it isn't.
edited 2 hours ago
Peter Mortensen
13.1k1983111
13.1k1983111
answered 8 hours ago
codelyzer
164114
164114
This is incorrect. "Whenever you use the second parameter, Python automatically converts the data type of the return value to float.". Please cite sources. In this case, attempting to cite sources will show that the return type behaves as Aniket describes (And cites withHelp(round)
)
â Nicholas Pipitone
31 mins ago
add a comment |Â
This is incorrect. "Whenever you use the second parameter, Python automatically converts the data type of the return value to float.". Please cite sources. In this case, attempting to cite sources will show that the return type behaves as Aniket describes (And cites withHelp(round)
)
â Nicholas Pipitone
31 mins ago
This is incorrect. "Whenever you use the second parameter, Python automatically converts the data type of the return value to float.". Please cite sources. In this case, attempting to cite sources will show that the return type behaves as Aniket describes (And cites with
Help(round)
)â Nicholas Pipitone
31 mins ago
This is incorrect. "Whenever you use the second parameter, Python automatically converts the data type of the return value to float.". Please cite sources. In this case, attempting to cite sources will show that the return type behaves as Aniket describes (And cites with
Help(round)
)â Nicholas Pipitone
31 mins ago
add a comment |Â
Peter Hofer is a new contributor. Be nice, and check out our Code of Conduct.
Peter Hofer is a new contributor. Be nice, and check out our Code of Conduct.
Peter Hofer is a new contributor. Be nice, and check out our Code of Conduct.
Peter Hofer is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52893372%2fround-returns-different-result-depending-on-the-number-of-arguments%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
I get 4.0 in both cases (python 3.5)
â taras
8 hours ago
5
+1 I was surprised by the downvote. This is a good question, with runnable code, probing a counterintuitive edge-case. I certainly learned something from this. Thanks, Peter.
â Bill Cheatham
8 hours ago
6
@taras: nope,
round(4.1)
in Python 3.5 produces4
, onlyround(4.1, 0)
produces4.0
. Do triple-check your Python versions. Useimport sys; print(sys.version_info)
from inside Python if you have to, because the behaviour you are reporting is specific to Python two. Theround()
function documentation for Python 3 covers this case explicitly: Ifndigits
is omitted or isNone
, it returns the nearest integer to its input..â Martijn Pietersâ¦
8 hours ago
1
@MartijnPieters, thank you for pointing it out. Apparently, I've checked it using 2.7 thinking I was running 3.5.
â taras
7 hours ago
How is this not a duplicate after more than 10 years and more than 15 million questions?
â Peter Mortensen
2 hours ago