round() in Python
Clash Royale CLAN TAG#URR8PPP
up vote
11
down vote
favorite
I'm currently learning python with pycharm as IDE. While using the round() function I noticed that I get two different results depending on wether 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
11
down vote
favorite
I'm currently learning python with pycharm as IDE. While using the round() function I noticed that I get two different results depending on wether 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
I get 4.0 in both cases (python 3.5)
â taras
1 hour ago
2
+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
1 hour ago
2
@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â¦
1 hour ago
@MartijnPieters, thank you for pointing it out. Apparently, I've checked it using 2.7 thinking I was running 3.5.
â taras
39 mins ago
add a comment |Â
up vote
11
down vote
favorite
up vote
11
down vote
favorite
I'm currently learning python with pycharm as IDE. While using the round() function I noticed that I get two different results depending on wether 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
I'm currently learning python with pycharm as IDE. While using the round() function I noticed that I get two different results depending on wether 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 1 hour ago
Martijn Pietersâ¦
681k12323332192
681k12323332192
New contributor
asked 1 hour ago
Peter Hofer
612
612
New contributor
New contributor
I get 4.0 in both cases (python 3.5)
â taras
1 hour ago
2
+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
1 hour ago
2
@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â¦
1 hour ago
@MartijnPieters, thank you for pointing it out. Apparently, I've checked it using 2.7 thinking I was running 3.5.
â taras
39 mins ago
add a comment |Â
I get 4.0 in both cases (python 3.5)
â taras
1 hour ago
2
+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
1 hour ago
2
@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â¦
1 hour ago
@MartijnPieters, thank you for pointing it out. Apparently, I've checked it using 2.7 thinking I was running 3.5.
â taras
39 mins ago
I get 4.0 in both cases (python 3.5)
â taras
1 hour ago
I get 4.0 in both cases (python 3.5)
â taras
1 hour ago
2
2
+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
1 hour 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
1 hour ago
2
2
@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â¦
1 hour 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â¦
1 hour ago
@MartijnPieters, thank you for pointing it out. Apparently, I've checked it using 2.7 thinking I was running 3.5.
â taras
39 mins ago
@MartijnPieters, thank you for pointing it out. Apparently, I've checked it using 2.7 thinking I was running 3.5.
â taras
39 mins ago
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
10
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.
2
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
1 hour ago
Might want to link to the docs and quote the first sentence.
â kabanus
58 mins ago
add a comment |Â
up vote
2
down vote
The round() function in python takes 2 parameters
1) number - number to be rounded
2) 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.
add a comment |Â
up vote
2
down vote
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
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
10
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.
2
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
1 hour ago
Might want to link to the docs and quote the first sentence.
â kabanus
58 mins ago
add a comment |Â
up vote
10
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.
2
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
1 hour ago
Might want to link to the docs and quote the first sentence.
â kabanus
58 mins ago
add a comment |Â
up vote
10
down vote
up vote
10
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 1 hour ago
Osuman AAA
277111
277111
2
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
1 hour ago
Might want to link to the docs and quote the first sentence.
â kabanus
58 mins ago
add a comment |Â
2
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
1 hour ago
Might want to link to the docs and quote the first sentence.
â kabanus
58 mins ago
2
2
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
1 hour 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
1 hour ago
Might want to link to the docs and quote the first sentence.
â kabanus
58 mins ago
Might want to link to the docs and quote the first sentence.
â kabanus
58 mins ago
add a comment |Â
up vote
2
down vote
The round() function in python takes 2 parameters
1) number - number to be rounded
2) 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.
add a comment |Â
up vote
2
down vote
The round() function in python takes 2 parameters
1) number - number to be rounded
2) 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.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
The round() function in python takes 2 parameters
1) number - number to be rounded
2) 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 2 parameters
1) number - number to be rounded
2) 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.
answered 1 hour ago
codelyzer
158114
158114
add a comment |Â
add a comment |Â
up vote
2
down vote
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
add a comment |Â
up vote
2
down vote
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
add a comment |Â
up vote
2
down vote
up vote
2
down vote
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
answered 14 mins ago
Aniket Navlur
16419
16419
add a comment |Â
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-in-python%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
I get 4.0 in both cases (python 3.5)
â taras
1 hour ago
2
+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
1 hour ago
2
@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â¦
1 hour ago
@MartijnPieters, thank you for pointing it out. Apparently, I've checked it using 2.7 thinking I was running 3.5.
â taras
39 mins ago