mIRC Home    About    Download    Register    News    Help

Print Thread
#15247 14/03/03 02:57 AM
Joined: Dec 2002
Posts: 68
P
Babel fish
OP Offline
Babel fish
P
Joined: Dec 2002
Posts: 68
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

Last edited by phrozenfire; 14/03/03 03:30 AM.
#15248 14/03/03 04:16 AM
Joined: Jan 2003
Posts: 44
L
Ameglian cow
Offline
Ameglian cow
L
Joined: Jan 2003
Posts: 44
can you post the solution anyways?

as it would help others wanting to learn.

thanks.

#15249 14/03/03 11:26 AM
Joined: Jan 2003
Posts: 2,523
Q
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
$bytes(34352462,b)


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
#15250 15/03/03 10:13 AM
Joined: Mar 2003
Posts: 32
P
Pap Offline
Ameglian cow
Offline
Ameglian cow
P
Joined: Mar 2003
Posts: 32
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.

#15251 15/03/03 10:19 AM
Joined: Dec 2002
Posts: 3,138
C
Hoopy frood
Offline
Hoopy frood
C
Joined: Dec 2002
Posts: 3,138
The $bytes example querty posted works for an infinite length number...

#15252 15/03/03 10:34 AM
Joined: Mar 2003
Posts: 32
P
Pap Offline
Ameglian cow
Offline
Ameglian cow
P
Joined: Mar 2003
Posts: 32
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.

#15253 15/03/03 10:36 AM
Joined: Mar 2003
Posts: 32
P
Pap Offline
Ameglian cow
Offline
Ameglian cow
P
Joined: Mar 2003
Posts: 32
Yeah, but his doesn't work with decimals now does it :P

#15254 16/03/03 02:25 AM
Joined: Jan 2003
Posts: 2,523
Q
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
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:

Last edited by qwerty; 16/03/03 02:29 AM.

/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
#15255 16/03/03 02:30 AM
Joined: Mar 2003
Posts: 32
P
Pap Offline
Ameglian cow
Offline
Ameglian cow
P
Joined: Mar 2003
Posts: 32
that or
Code:
alias commas return $bytes($1,b) $+ $right($calc($1 - $int($1)),-1)

#15256 16/03/03 04:03 AM
Joined: Jan 2003
Posts: 2,523
Q
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
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.


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
#15257 02/04/04 11:16 PM
Joined: Feb 2004
Posts: 2,019
Hoopy frood
Offline
Hoopy frood
Joined: Feb 2004
Posts: 2,019
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


Gone.

Link Copied to Clipboard