round() returns different result depending on the number of arguments

The name of the pictureThe name of the pictureThe name of the pictureClash 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?










share|improve this question









New contributor




Peter Hofer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 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 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




    @MartijnPieters, thank you for pointing it out. Apparently, I've checked it using 2.7 thinking I was running 3.5.
    – taras
    8 hours ago










  • How is this not a duplicate after more than 10 years and more than 15 million questions?
    – Peter Mortensen
    2 hours ago














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?










share|improve this question









New contributor




Peter Hofer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 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 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




    @MartijnPieters, thank you for pointing it out. Apparently, I've checked it using 2.7 thinking I was running 3.5.
    – taras
    8 hours ago










  • How is this not a duplicate after more than 10 years and more than 15 million questions?
    – Peter Mortensen
    2 hours ago












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?










share|improve this question









New contributor




Peter Hofer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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






share|improve this question









New contributor




Peter Hofer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Peter Hofer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 23 mins ago









Aran-Fey

20.3k53166




20.3k53166






New contributor




Peter Hofer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 8 hours ago









Peter Hofer

1144




1144




New contributor




Peter Hofer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Peter Hofer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Peter Hofer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 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 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




    @MartijnPieters, thank you for pointing it out. Apparently, I've checked it using 2.7 thinking I was running 3.5.
    – taras
    8 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




    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 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




    @MartijnPieters, thank you for pointing it out. Apparently, I've checked it using 2.7 thinking I was running 3.5.
    – taras
    8 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
8 hours ago




@MartijnPieters, thank you for pointing it out. Apparently, I've checked it using 2.7 thinking I was running 3.5.
– taras
8 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












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





share|improve this answer






















  • I had no idea you could use negative numbers on round(). Good to know!
    – Daffy
    53 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
    40 mins ago

















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.






share|improve this answer
















  • 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






  • 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 an if statement checking for args
    – cat
    6 hours ago

















up vote
1
down vote













The round() function in Python takes two 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.






share|improve this answer






















  • 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
    41 mins ago











Your Answer





StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);






Peter Hofer is a new contributor. Be nice, and check out our Code of Conduct.









 

draft saved


draft discarded


















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






























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





share|improve this answer






















  • I had no idea you could use negative numbers on round(). Good to know!
    – Daffy
    53 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
    40 mins ago














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





share|improve this answer






















  • I had no idea you could use negative numbers on round(). Good to know!
    – Daffy
    53 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
    40 mins ago












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





share|improve this answer














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






share|improve this answer














share|improve this answer



share|improve this answer








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 on round(). Good to know!
    – Daffy
    53 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
    40 mins ago
















  • I had no idea you could use negative numbers on round(). Good to know!
    – Daffy
    53 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
    40 mins ago















I had no idea you could use negative numbers on round(). Good to know!
– Daffy
53 mins ago




I had no idea you could use negative numbers on round(). Good to know!
– Daffy
53 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
40 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
40 mins ago












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.






share|improve this answer
















  • 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






  • 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 an if statement checking for args
    – cat
    6 hours ago














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.






share|improve this answer
















  • 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






  • 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 an if statement checking for args
    – cat
    6 hours ago












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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










answered 8 hours ago









Osuman AAA

295111




295111







  • 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






  • 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 an if statement checking for args
    – cat
    6 hours ago












  • 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






  • 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 an if statement checking for args
    – 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










up vote
1
down vote













The round() function in Python takes two 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.






share|improve this answer






















  • 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
    41 mins ago















up vote
1
down vote













The round() function in Python takes two 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.






share|improve this answer






















  • 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
    41 mins ago













up vote
1
down vote










up vote
1
down vote









The round() function in Python takes two 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.






share|improve this answer














The round() function in Python takes two 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.







share|improve this answer














share|improve this answer



share|improve this answer








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 with Help(round))
    – Nicholas Pipitone
    41 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
    41 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
41 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
41 mins ago











Peter Hofer is a new contributor. Be nice, and check out our Code of Conduct.









 

draft saved


draft discarded


















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.













 


draft saved


draft discarded














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













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?