| View previous topic :: View next topic |
| Author |
Message |
heman Voice
Joined: 30 Dec 2006 Posts: 13
|
Posted: Sat Feb 03, 2007 3:00 pm Post subject: calculate % between two rows |
|
|
I have to colums: "nm" and "update"
To count the inputs i use the folowing codes:
| Code: |
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:
| Quote: |
<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:
| Quote: |
<botname> 100 releases in db, 50 are updated (50%)
|
Thanks |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Sat Feb 03, 2007 4:41 pm Post subject: |
|
|
You probably would like to investigate the expr command. _________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
heman Voice
Joined: 30 Dec 2006 Posts: 13
|
Posted: Sun Feb 04, 2007 7:14 pm Post subject: |
|
|
I tried
| Code: |
set perc [expr { $cnupdt / $cnt * 100 }]
|
but results is : 0
Am I missing something? |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Sun Feb 04, 2007 8:08 pm Post subject: |
|
|
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: | | set perc [expr {($cnupdt*100)/$cnt}] |
_________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
heman Voice
Joined: 30 Dec 2006 Posts: 13
|
Posted: Mon Feb 05, 2007 5:44 am Post subject: |
|
|
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
| Quote: |
expr 5 / 4
returns 1, while
expr 5 / 4.0
return 1.25
|
I tried
| Code: |
set perc [expr {($cnupdt.0*100)/$cnt}]
instead of
set perc [expr {($cnupdt*100)/$cnt}]
|
But that didnt worked. |
|
| Back to top |
|
 |
user

Joined: 18 Mar 2003 Posts: 1452 Location: Norway
|
Posted: Mon Feb 05, 2007 6:08 am Post subject: |
|
|
add the ".0" to your static value (100) or remove the braces...
| Code: | % 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"? |
|
| Back to top |
|
 |
heman Voice
Joined: 30 Dec 2006 Posts: 13
|
Posted: Mon Feb 05, 2007 6:25 am Post subject: |
|
|
ok that works, thanks.
Final question
the output now looks like for example:
Is it possible it wil only announce the first one or two after the "." ?
So someting like:
And thanks for all the help |
|
| Back to top |
|
 |
Sir_Fz Revered One

Joined: 27 Apr 2003 Posts: 3793 Location: Lebanon
|
Posted: Mon Feb 05, 2007 3:38 pm Post subject: |
|
|
| Quote: | % format %.2f 2.85714285714
2.86 |
Edit: user did include that in his code  _________________ Follow me on GitHub
- Opposing
Public Tcl scripts |
|
| Back to top |
|
 |
metroid Owner
Joined: 16 Jun 2004 Posts: 771
|
Posted: Tue Feb 06, 2007 2:24 pm Post subject: |
|
|
| Using braces in expr is faster so I'd recommend using the first option. |
|
| Back to top |
|
 |
|