Re: Procedure for percent of a value
- From: Neil Madden <nem@xxxxxxxxxxxxx>
- Date: Fri, 29 Feb 2008 15:35:02 +0000
Marcos Martinez Sancho wrote:
Hello,
I need a small procedure for calculate the percent of a value
e.g.
set srate [expr int(($tps_ok / $number_of_total_tps)*100)]
puts "\nTEST RESULT: $number_of_total_tps tps of which $tps_ok PASSED and $tps_nok FAILED so $srate % succes rate"
doesnt look optimal and is not working except when srate = 100%. Otherwise, I get 0%.
You need to ensure floating point division as others have indicated. The best way to achieve this is:
proc percent {num total} {
expr {double($num)/double($total) * 100}
}
This will return a floating-point number. When you display that number, use [format] to achieve any rounding, e.g.:
format "%.0f%%" [percent 7 30]
-- Neil
.
- Follow-Ups:
- Re: Procedure for percent of a value
- From: suchenwi
- Re: Procedure for percent of a value
- References:
- Procedure for percent of a value
- From: Marcos Martinez Sancho
- Procedure for percent of a value
- Prev by Date: Re: Procedure for percent of a value
- Next by Date: Re: Tk8.5: Tk_PhotoSetSize vs Tk_PhotoSetSize_Panic
- Previous by thread: Re: Procedure for percent of a value
- Next by thread: Re: Procedure for percent of a value
- Index(es):
Relevant Pages
|