Re: print()

Discussion in 'Python' started by Lie Ryan, Oct 17, 2009.

  1. Lie Ryan

    Lie Ryan Guest

    mattia wrote:
    > Another question (always py3). How can I print only the first number
    > after the comma of a division?
    > e.g. print(8/3) --> 2.66666666667
    > I just want 2.6 (or 2.66)


    Are you sure you don't want that to be 2.7 or 2.67? Then you can use:
    n = int(n * 10**2) / 10**2
    else if 2.7 pr 2.67 is what you wanted, you could use:
    n = round(n, 2)
     
    Lie Ryan, Oct 17, 2009
    #1
    1. Advertising

  2. Lie Ryan

    Dave Angel Guest

    Lie Ryan wrote:
    > mattia wrote:
    >> Another question (always py3). How can I print only the first number
    >> after the comma of a division?
    >> e.g. print(8/3) --> 2.66666666667
    >> I just want 2.6 (or 2.66)

    >
    > Are you sure you don't want that to be 2.7 or 2.67? Then you can use:
    > n = int(n * 10**2) / 10**2
    > else if 2.7 pr 2.67 is what you wanted, you could use:
    > n = round(n, 2)
    >
    >

    Bad idea to use round() to make numbers format properly. No guarantees
    that the print logic will then print the number rounded the way you
    want. It could just as easily do 2.69999999999999 when you finally go
    to print it.

    Best to use format(), the way it was intended. Round to decimal while
    converting to decimal. Otherwise surprises await in dark corners.

    DaveA
     
    Dave Angel, Oct 17, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bad_Kid
    Replies:
    3
    Views:
    3,963
    Shawn H. Mesiatowsky
    Apr 7, 2005
  2. Bryan Shaw

    Lock Print Screen and File Print

    Bryan Shaw, Apr 19, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    3,970
    Kevin Spencer
    Apr 20, 2005
  3. EDom
    Replies:
    0
    Views:
    448
  4. keto
    Replies:
    0
    Views:
    1,000
  5. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    374
    David Cournapeau
    Dec 30, 2008
Loading...

Share This Page