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.

calculate % between two rows

Help for those learning Tcl or writing their own scripts.
Post Reply
h
heman
Voice
Posts: 13
Joined: Sat Dec 30, 2006 3:17 pm

calculate % between two rows

Post by heman »

I have to colums: "nm" and "update"

To count the inputs i use the folowing codes:

Code: Select all

set cnt [mysqlsel $db "select count(*) from $table WHERE nm = '$text';" -list]
set cnupdt [mysqlsel $db "select count(*) from $table WHERE update = '$text';" -list]
This gives announcemnet liek this:
<botname> 100 releases in db, 50 are updated
Now I want to make a variable that will tell the percentage updates are made from nm

Someting like:
<botname> 100 releases in db, 50 are updated (50%)
Thanks
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

You probably would like to investigate the expr command.
NML_375
h
heman
Voice
Posts: 13
Joined: Sat Dec 30, 2006 3:17 pm

Post by heman »

I tried

Code: Select all

set perc [expr { $cnupdt / $cnt * 100 }]
but results is : 0

Am I missing something?
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Yup, type conversion and order of operations.
With tcl, if you divide two integers, the result will also be an integer (using truncation). And since percent is less than 1, you'll always get 0.

You'll either have to do the multiplication first, or convert the numbers to floats (I'd say the first is easier)

Try this:

Code: Select all

set perc [expr {($cnupdt*100)/$cnt}]
NML_375
h
heman
Voice
Posts: 13
Joined: Sat Dec 30, 2006 3:17 pm

Post by heman »

ok that works, sort of.

cnupdt = 7
cnt =175

7 x 100 / 175 = 4 so it announces -> 4


cnupdt = 5
cnt =175

5 x 100 / 175 = 2,86 but it announces -> 2

Is there a way it announce -> 2,86 ?


http://www.tcl.tk/man/tcl8.4/TclCmd/expr.htm
expr 5 / 4

returns 1, while

expr 5 / 4.0

return 1.25
I tried

Code: Select all

set perc [expr {($cnupdt.0*100)/$cnt}]

instead of

set perc [expr {($cnupdt*100)/$cnt}]
But that didnt worked.
User avatar
user
&nbsp;
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

add the ".0" to your static value (100) or remove the braces...

Code: Select all

% set a 3; set b 7
% format %.2f [expr {($a*100.0)/$b}]
42.86
% format %.2f [expr ($a.0*100)/$b]
42.86
Have you ever read "The Manual"?
h
heman
Voice
Posts: 13
Joined: Sat Dec 30, 2006 3:17 pm

Post by heman »

ok that works, thanks.

Final question :D

the output now looks like for example:
2.85714285714 %
Is it possible it wil only announce the first one or two after the "." ?

So someting like:
2.85 %
And thanks for all the help
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

% format %.2f 2.85714285714
2.86
Edit: user did include that in his code ;)
m
metroid
Owner
Posts: 771
Joined: Wed Jun 16, 2004 2:46 am

Post by metroid »

Using braces in expr is faster so I'd recommend using the first option.
Post Reply