Re: Procedure for percent of a value



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
.



Relevant Pages

  • Re: Procedure for percent of a value
    ... $tps_nok FAILED so $srate % succes rate" ... doesnt look optimal and is not working except when srate = 100%. ... Tom K. ...
    (comp.lang.tcl)
  • Re: Procedure for percent of a value
    ... old fogies who have done a lot of work on integer only systems is ... to multiply first ie.g. ... doesnt look optimal and is not working except when srate = 100%. ... The solution is to make one of the values a floating point value, all other numbers will be "coerced" into floating point numbers too. ...
    (comp.lang.tcl)
  • Re: Procedure for percent of a value
    ... doesnt look optimal and is not working except when srate = 100%. ... When both numbers in a division operation are are integer, [expr] ... The solution is to make one of the values a floating point value, ... 'Everybody understand that floating point calculations are NOT ...
    (comp.lang.tcl)
  • Re: Procedure for percent of a value
    ... Marcos Martinez Sancho wrote: ... doesnt look optimal and is not working except when srate = 100%. ... performs an integer division. ... The solution is to make one of the values a floating point value, all other numbers will be "coerced" into floating point numbers too. ...
    (comp.lang.tcl)