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.

Percents

Old posts that have not been replied to for several years.
Locked
B
BiLL
Halfop
Posts: 78
Joined: Wed Sep 26, 2001 8:00 pm
Location: Germany

Percents

Post by BiLL »

Hi,

got a question about percents!

set percent1 [expr (100 / 9.0) * 7]
set percent2 [expr (100 / 9.0) * 2]

that returns
percent1 = 77.7777777778%
percent2 = 22.2222222222%

Can anyone help me to make percent1 = 77,8% and percent 2 = 22,2% ?
Would be great - thanks!!
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

Nice, I come by and see this.

This should do you. However, it isn't same, and you need to make sure that what it being passed to the proc, is infact a number.

Code: Select all

proc round {in} {
  return [expr (round($in * 10) +0.0) / 10]
}
It will allways return a floating point value

Examples
round 51 = 51.0
round 51.5 = 51.5
round 51.55 = 51.6
round 51.545 = 51.5
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

either

Code: Select all

format %.1f <insert result here>
(format will just chop off extra digits, so the result might not be what you want.) or

Code: Select all

set tcl_precision 1
before the expr...but that will result in every calculation having this lousy precision :P

EDIT: format does rounding..so go ahead and use it :)
B
BiLL
Halfop
Posts: 78
Joined: Wed Sep 26, 2001 8:00 pm
Location: Germany

Post by BiLL »

awesome
thanks user
and thanks ppslim for that proc - WORKS GREAT!
Locked