Number formatting

By KuTsuM on Oct 22, 2005

This is sort of stupid (as in rather basic), but I figure someone could learn from this.

Usage:
$min(numbers sepearted by spaces)
$max(numbers sepearted by spaces)
$average(numbers sepearted by spaces)
$decimal(number of decimal places, number) [IE: $decimal(2, 5.546) will return 5.55]

alias min {
  var %num = $1-,%x = 1,%min
  while (%x <= $numtok(%num,32)) {
    var %tok = $gettok(%num,%x,32)
    if (!%min) || (%tok < %min) { %min = %tok }
    inc %x
  }
  return %min
}
alias max {
  var %num = $1-,%x = 1,%max
  while (%x <= $numtok(%num,32)) {
    var %tok = $gettok(%num,%x,32)
    if (!%max) || (%tok > %max) { %max = %tok }
    inc %x
  }
  return %max
} 
alias average {
  var %num = $1-,%x = 1,%total,%tok
  while (%x <= $numtok(%num,32)) {
    %tok = $gettok(%num,%x,32)
    %total = $calc(%total + %tok)
    inc %x
  }
  return $calc(%total / %x)
}
alias decimal {
    if ($2 isnum) {
      var %tok = $gettok($2,2,46),%tokn = $gettok($2,1,46)
      if ($1 = 0) { return %tokn | halt }
      return %tokn $+ . $+ $iif($right($left(%tok,$calc($1 + 1)),1) >= 5,$calc($left(%tok,$1) + 1),$left(%tok,$1))
  }
}

Comments

Sign in to comment.
xDaeMoN   -  Oct 23, 2005

You can also simplify your $min & $max alias to this ( which you can also specifify the token delimiters):

;Syntax: $min(n1 n2 n3,C)
alias min return $gettok($sorttok($$1,$2,n),1,$2)

;Syntax: $max(n1 n2 n3,C)
alias max return $gettok($sorttok($$1,$2,nr),1,$2)

For your $decimal, $round is already built-in with mIRC which does the same thing.

 Respond  
tank59   -  Oct 23, 2005

Your min and max identifiers don\'t seem to work. The average and decimal seem to work properly, so nice job on those. I noticed that in the average alias you have a unused variable(%div) you might want to take out.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.