# round not rounding to 0 places

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

1. ### FuzzydaveGuest

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

2. ### Sybren StuvelGuest

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

3. ### Simon FormanGuest

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
4. ### Tim LeslieGuest

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
5. ### FuzzydaveGuest

> 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
6. ### Cameron LairdGuest

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
7. ### Sybren StuvelGuest

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