round not rounding to 0 places

Discussion in 'Python' started by Fuzzydave, Aug 16, 2006.

  1. Fuzzydave

    Fuzzydave Guest

    I have been using a round command in a few places to round
    a value to zero decimal places using the following format,

    round('+value+', 0)

    but this consistantly returns the rounded result of the value
    to one decimal place with a zero

    EG:

    4.97 is returned as 5.0 when i want it returned as 5, does
    anyone know why this is and if i can get the round to make
    the value 5?

    David P
    Fuzzydave, Aug 16, 2006
    #1
    1. Advertising

  2. Fuzzydave enlightened us with:
    > round('+value+', 0)
    >
    > 4.97 is returned as 5.0 when i want it returned as 5


    round returns a float. Use

    int(round('+value+', 0))

    to get an integer.

    Sybren
    --
    The problem with the world is stupidity. Not saying there should be a
    capital punishment for stupidity, but why don't we just take the
    safety labels off of everything and let the problem solve itself?
    Frank Zappa
    Sybren Stuvel, Aug 16, 2006
    #2
    1. Advertising

  3. Fuzzydave

    Simon Forman Guest

    Fuzzydave wrote:
    > I have been using a round command in a few places to round
    > a value to zero decimal places using the following format,
    >
    > round('+value+', 0)
    >
    > but this consistantly returns the rounded result of the value
    > to one decimal place with a zero
    >
    > EG:
    >
    > 4.97 is returned as 5.0 when i want it returned as 5, does
    > anyone know why this is and if i can get the round to make
    > the value 5?
    >
    > David P


    |>> n = 4.97
    |>> round(n)
    5.0
    |>> int(round(n))
    5
    |>> help(round)
    Help on built-in function round in module __builtin__:

    round(...)
    round(number[, ndigits]) -> floating point number

    Round a number to a given precision in decimal digits (default 0
    digits).
    This always returns a floating point number. Precision may be
    negative.


    HTH,
    ~Simon


    BTW, '+value+' ..? Huh?
    Simon Forman, Aug 16, 2006
    #3
  4. Fuzzydave

    Tim Leslie Guest

    On 16 Aug 2006 00:19:24 -0700, Fuzzydave <> wrote:
    > I have been using a round command in a few places to round
    > a value to zero decimal places using the following format,
    >
    > round('+value+', 0)
    >
    > but this consistantly returns the rounded result of the value
    > to one decimal place with a zero
    >
    > EG:
    >
    > 4.97 is returned as 5.0 when i want it returned as 5, does
    > anyone know why this is and if i can get the round to make
    > the value 5?


    round returns a float. You probably want to convert it to an int.

    >>> int(round(4.97))

    5

    Cheers,

    Tim

    >
    > David P
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
    Tim Leslie, Aug 16, 2006
    #4
  5. Fuzzydave

    Fuzzydave Guest

    > Sybren Stuvel wrote:
    > round returns a float. Use
    > int(round('+value+', 0))
    > to get an integer.
    > Sybren


    ahh of course it does, slaps own forehead sorted
    thanks :)

    David P
    Fuzzydave, Aug 16, 2006
    #5
  6. In article <>,
    Tim Leslie <> wrote:
    >On 16 Aug 2006 00:19:24 -0700, Fuzzydave <> wrote:
    >> I have been using a round command in a few places to round
    >> a value to zero decimal places using the following format,
    >>
    >> round('+value+', 0)
    >>
    >> but this consistantly returns the rounded result of the value
    >> to one decimal place with a zero
    >>
    >> EG:
    >>
    >> 4.97 is returned as 5.0 when i want it returned as 5, does
    >> anyone know why this is and if i can get the round to make
    >> the value 5?

    >
    >round returns a float. You probably want to convert it to an int.
    >
    >>>> int(round(4.97))

    >5

    .
    .
    .
    I'm surprised no one has recommended

    "%.0f" % 4.97
    Cameron Laird, Aug 16, 2006
    #6
  7. Cameron Laird enlightened us with:
    > I'm surprised no one has recommended
    >
    > "%.0f" % 4.97


    Probably because that returns '5', not 5 ;-)

    Sybren
    --
    The problem with the world is stupidity. Not saying there should be a
    capital punishment for stupidity, but why don't we just take the
    safety labels off of everything and let the problem solve itself?
    Frank Zappa
    Sybren Stuvel, Aug 16, 2006
    #7
    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. Gizmo
    Replies:
    1
    Views:
    3,573
    Jakob Bieling
    Aug 31, 2003
  2. Pat Maddox

    Round floats to N decimal places?

    Pat Maddox, Mar 22, 2006, in forum: Ruby
    Replies:
    4
    Views:
    176
  3. ddoherty03
    Replies:
    4
    Views:
    93
    ddoherty03
    Jul 23, 2009
  4. Dr John Stockton

    Rounding a Number to a String with N decimal places

    Dr John Stockton, Aug 15, 2004, in forum: Javascript
    Replies:
    1
    Views:
    91
    G Roydor
    Aug 15, 2004
  5. Gary Mayor

    Rounding up to 2 decimal places

    Gary Mayor, Dec 9, 2003, in forum: Perl Misc
    Replies:
    6
    Views:
    218
    Anno Siegel
    Dec 10, 2003
Loading...

Share This Page