This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Rounding to x places?

Old posts that have not been replied to for several years.
Locked
D
DayCuts
Voice
Posts: 37
Joined: Tue Jun 15, 2004 8:43 am

Rounding to x places?

Post by DayCuts »

I have looked through the tcl/tk help and tried using expr round(), this works of corse. However i seem unabled to round accurately to a specific X places. I have seen in a few places people use things such as [expr round(9.345) * 1.0] or similar things, but such workarounds are not very accurate as you can see simply by looking at the logic in the math.

Can anybody help me out with a more accurate method of rounding? TIA
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

format %.<number of decimals>f <value>
eg:

Code: Select all

format %.2f 4.567
returns 4.57
Have you ever read "The Manual"?
D
DayCuts
Voice
Posts: 37
Joined: Tue Jun 15, 2004 8:43 am

Post by DayCuts »

Ah, thank you user. I guess i just didn't relate format to string/math manipulation.
User avatar
awyeah
Revered One
Posts: 1580
Joined: Mon Apr 26, 2004 2:37 am
Location: Switzerland
Contact:

Post by awyeah »

Actually its the same as in C, if you take a closer look.

%2f = 2 decimal places
%3f = 3 decimal places... and so on.
·­awyeah·

==================================
Facebook: jawad@idsia.ch (Jay Dee)
PS: Guys, I don't accept script helps or requests personally anymore.
==================================
User avatar
user
&nbsp;
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

awyeah wrote:%2f = 2 decimal places
Nope.
Have you ever read "The Manual"?
Locked