f



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

Hello Group

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

Thanks
Evilson
0
Evil
10/16/2010 8:45:41 AM
comp.lang.tcl 23410 articles. 2 followers. Post Follow

3 Replies
1718 Views

Similar Articles

[PageSpeed] 31

Evil Son <ewilsonmail@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                          asnd@triumf.ca
0
Donald
10/16/2010 10:24:21 AM
On 16 Pa=C5=BA, 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
0
10/16/2010 2:08:44 PM
On Oct 17, 12:08=C2=A0am, Googie <pawelsal...@gmail.com> wrote:
> On 16 Pa=C5=BA, 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

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
10/16/2010 4:10:40 PM
Reply: