Velocity Reviews > round not rounding to 0 places

# round not rounding to 0 places

Fuzzydave
Guest
Posts: n/a

 08-16-2006
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

Sybren Stuvel
Guest
Posts: n/a

 08-16-2006
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

Simon Forman
Guest
Posts: n/a

 08-16-2006
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?

Tim Leslie
Guest
Posts: n/a

 08-16-2006
On 16 Aug 2006 00:19:24 -0700, Fuzzydave <(E-Mail Removed)> 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
>

Fuzzydave
Guest
Posts: n/a

 08-16-2006
> 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

Cameron Laird
Guest
Posts: n/a

 08-16-2006
In article <(E-Mail Removed)>,
Tim Leslie <(E-Mail Removed)> wrote:
>On 16 Aug 2006 00:19:24 -0700, Fuzzydave <(E-Mail Removed)> 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

Sybren Stuvel
Guest
Posts: n/a

 08-16-2006
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

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Marc Heiler Ruby 7 01-02-2010 12:08 PM ddoherty03 Ruby 4 07-23-2009 02:55 PM Dr John Stockton Javascript 1 08-15-2004 06:17 PM Gary Mayor Perl Misc 6 12-10-2003 10:55 AM Gizmo C++ 1 08-31-2003 02:08 PM