round() in Python

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










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.



















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














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?










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.



















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












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?










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.











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






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 1 hour ago









Martijn Pieters♦

681k12323332192




681k12323332192






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 1 hour ago









Peter Hofer

612




612




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.











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
















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















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












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.






share|improve this answer
















  • 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










  • Might want to link to the docs and quote the first sentence.
    – kabanus
    58 mins ago

















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.






share|improve this answer



























    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





    share|improve this answer




















      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-in-python%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
      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.






      share|improve this answer
















      • 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










      • Might want to link to the docs and quote the first sentence.
        – kabanus
        58 mins ago














      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.






      share|improve this answer
















      • 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










      • Might want to link to the docs and quote the first sentence.
        – kabanus
        58 mins ago












      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.






      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 1 hour ago









      Osuman AAA

      277111




      277111







      • 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










      • Might want to link to the docs and quote the first sentence.
        – kabanus
        58 mins ago












      • 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










      • 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












      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.






      share|improve this answer
























        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.






        share|improve this answer






















          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.






          share|improve this answer












          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 1 hour ago









          codelyzer

          158114




          158114




















              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





              share|improve this answer
























                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





                share|improve this answer






















                  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





                  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










                  answered 14 mins ago









                  Aniket Navlur

                  16419




                  16419




















                      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-in-python%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Comments

                      Popular posts from this blog

                      Long meetings (6-7 hours a day): Being “babysat” by supervisor

                      Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                      Confectionery