Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Rounding (up/down) to a certain number of decimal places

2,070 views
Skip to first unread message

Evil Son

unread,
Oct 16, 2010, 4:45:41 AM10/16/10
to
Hello Group

How do I round say 1.23 up/down to 2 decimal places? I am currently
(ab)using [format].

Thanks
Evilson

Donald Arseneau

unread,
Oct 16, 2010, 6:24:21 AM10/16/10
to
Evil Son <ewils...@gmail.com> writes:

> How do I round say 1.23 up/down to 2 decimal places? I am currently
> (ab)using [format].

Sounds like the right choice.

--
Donald Arseneau as...@triumf.ca

Googie

unread,
Oct 16, 2010, 10:08:44 AM10/16/10
to
On 16 Paź, 10:45, Evil Son <ewilsonm...@gmail.com> wrote:
> Hello Group
>
> How do I round say 1.23 up/down to 2 decimal places? I am currently
> (ab)using [format].

Using [format] is completly correct way, but if you prefer to avoid it
for some reason, there is other way:

set precision 2
set roundedValue [expr { double(round($value * pow(10,$precision))) /
pow(10,$precision) }]

Regards,
Googie

Evil Son

unread,
Oct 16, 2010, 12:10:40 PM10/16/10
to

Thanks Donald and Googie ... least of all for figuring out what I
meant. My example should have been 1.234 :P
Yes [format] works fine on my machine but I was just wondering about
whether it would behave the same on other machines and whether that
was the correct way to do it. Sounds like it is. (IIRC, C's <stdio.h>
printf's behaviour is undefined here. But this is Tcl of course ...
and my C is rusty.)

Thanks again.
Evilson

0 new messages