mIRC Homepage
Posted By: phrozenfire Comma's in numbers - 14/03/03 02:57 AM
NEVERMIND! I GOT THE ANSWER. Thank you xOliRu

I have a number stored in %calc.temp3. I want to make it so that every 3 digits (starting from the right) there is placed a comma, changing:
5566431
to
5,566,431

Thanks.

Note: The most there will ever be is 2 commas, but not always that many.

NEVERMIND! I GOT THE ANSWER. Thank you xOliRu
Posted By: lambroger Re: Comma's in numbers - 14/03/03 04:16 AM
can you post the solution anyways?

as it would help others wanting to learn.

thanks.
Posted By: qwerty Re: Comma's in numbers - 14/03/03 11:26 AM
$bytes(34352462,b)
Posted By: Pap Re: Comma's in numbers - 15/03/03 10:13 AM
Well, I made this a few days ago. The only problem is I have the amount of commas to add hardcoded in based on its lenth. I'm sure you could use while loops, but in my case it would use up more code than hardcoding it, because I don't anticipate it ever being 1 trillion or above. grin

Usage: either /commas 1234 or $commas(1234) in a script.
Code:
alias commas {
  if ( 3 < $len($1) && $len($1) < 7 ) {
    return $left($1,-3) $+ , $+ $right($1,3)
  }
  elseif ( 6 < $len($1) && $len($1) < 10 ) {
    return $left($1,-6) $+ , $+ $mid($1,-6,-3) $+ , $+ $right($1,3)
  }
  elseif ( 9 < $len($1) && $len($1) < 13 ) {
    return $left($1,-9) $+ , $+ $mid($1,-9,-6) $+ , $+ $mid($1,-6,-3) $+ , $+ $right($1,3)
  }
  else {
    return $1
  }
}

If you care to revise it so it works with an infinite-length (gulp) string, please post whatever you can think of.
Posted By: Collective Re: Comma's in numbers - 15/03/03 10:19 AM
The $bytes example querty posted works for an infinite length number...
Posted By: Pap Re: Comma's in numbers - 15/03/03 10:34 AM
And if you wanted to keep decimal places, the best way would probably be to do something like:
Code:
commas {
  .var %int $int($1)
  .var %decimals $right($calc($1 - $int($1)),-1)
  if ( 3 < $len(%int) && $len(%int) < 7 ) {
    return $left(%int,-3) $+ , $+ $right(%int,3) $+ %decimals
  }
  elseif ( 6 < $len(%int) && $len(%int) < 10 ) {
    return $left(%int,-6) $+ , $+ $mid(%int,-6,-3) $+ , $+ $right(%int,3) $+ %decimals
  }
  elseif ( 9 < $len(%int) && $len(%int) < 13 ) {
    return $left(%int,-9) $+ , $+ $mid(%int,-9,-6) $+ , $+ $mid(%int,-6,-3) $+ , $+ $right(%int,3) $+ %decimals
  }
  else {
    return $1
  }
}

Again, redundance, I know. But hey, it works, and it works darn well.
Posted By: Pap Re: Comma's in numbers - 15/03/03 10:36 AM
Yeah, but his doesn't work with decimals now does it :P
Posted By: qwerty Re: Comma's in numbers - 16/03/03 02:25 AM
Code:
alias commas return $bytes($1,b) $+ $mid($1,$pos($1,.))

or even shorter:
Code:
alias commas return $puttok($1,$bytes($1,b),1,46)

:tongue:
Posted By: Pap Re: Comma's in numbers - 16/03/03 02:30 AM
that or
Code:
alias commas return $bytes($1,b) $+ $right($calc($1 - $int($1)),-1)
Posted By: qwerty Re: Comma's in numbers - 16/03/03 04:03 AM
That works too, except that it is subject to rounding problems, since $calc() rounds up >6 decimals. Try your alias with the number 234234.0000004 to see what I mean.
Posted By: FiberOPtics Re: Comma's in numbers - 02/04/04 11:16 PM
Hi,

that alias doesn't work in the following cases:

$commas(8.6) will return 9.6
$commas(0.5) will return 1.5
$commas(-18.235) will return -19.735

In other words: when the number is a decimal,
and the part before the comma is in the interval [10, -infinite], and the first number after the comma, is higher than 5, it will increase (decrease) the final number with one

Grtz
© mIRC Discussion Forums