mIRC Home    About    Download    Register    News    Help

Print Thread
Formatting a number #182020 03/08/07 02:54 PM
Joined: Aug 2007
Posts: 2
A
afterlife123 Offline OP
Bowl of petunias
OP Offline
Bowl of petunias
A
Joined: Aug 2007
Posts: 2
Does anyone have/know the code to format a number out of and then back into a number in 1000's?

E.G Say you have an ever changing number E.G:
75,602
1,503,599
1,505

Would anyone be able to help me out in to change the number (ever changing) into E.G

75602
1503599
1505

:?

If you can also supply the code to be able to format the number back into a number with a , in the number would be helpful :P

Re: Formatting a number [Re: afterlife123] #182021 03/08/07 03:34 PM
Joined: Dec 2002
Posts: 2,962
S
starbucks_mafia Offline
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 2,962
Here's a couple of simple aliases to format and un-format numbers:

Code:
alias num_format return $bytes($1, b) $+ $iif($int($1) != $1, $+(.,$gettok($1,2,46)), $null)

alias num_unformat return $remove($1-, $chr(44), $chr(32))


Just put those in the Remotes section of the script editor (Alt+R).

eg.
$num_format(67475) returns 67,475
$num_unformat(43,644) returns 43644


Spelling mistakes, grammatical errors, and stupid comments are intentional.
Re: Formatting a number [Re: starbucks_mafia] #182024 03/08/07 04:22 PM
Joined: Aug 2004
Posts: 7,252
R
RusselB Offline
Hoopy frood
Offline
Hoopy frood
R
Joined: Aug 2004
Posts: 7,252
Alternative suggestion to the first alias

Code:
alias num_format return $bytes($1,bd)


The d parameter will keep the decimals, if there are any.

Re: Formatting a number [Re: afterlife123] #182027 03/08/07 04:57 PM
Joined: Aug 2007
Posts: 2
A
afterlife123 Offline OP
Bowl of petunias
OP Offline
Bowl of petunias
A
Joined: Aug 2007
Posts: 2
Cheers <3 blush

Re: Formatting a number [Re: RusselB] #182034 03/08/07 06:08 PM
Joined: Jan 2004
Posts: 509
L
LostShadow Offline
Fjord artisan
Offline
Fjord artisan
L
Joined: Jan 2004
Posts: 509
Thanks to RusselB for giving me another $comma() identifier.

Now I have 6. (But not all of them work efficiently.)

Code:
comma {
  var %format1 = $1
  var %format3 = $calc($len($1) + 1)
  while (%format3 > 1) {
    dec %format3
    if ($len(%format6) == 3) {
      var %format5 = $chr(44) $+ %format6 $+ %format5
      unset %format6
    }
    var %format6 = $mid(%format1,%format3,1) $+ %format6
  }
  return %format6 $+ %format5
}
comma2 {
  tokenize 46 $1 
  var %i = 1,%d,%n 
  while ($mid($1,- $+ %i,1) isnum) { 
    var %d = $ifmatch,%n = $+($iif(3 // %i,$chr(44)),%d,%n) 
    inc %i 
  } 
  return $iif($chr(44) $+ * iswm %n,$mid(%n,2),%n) $+ $iif($2,. $+ $2)
}
comma3 {
  var %z 
  return $(,,$regsub($1,/(^\d+?(?=(?>(?:\d{3})+)(?=\.|$))|\G\d{3}(?=\d))/g,\1 $+ $chr(44),%z)) %z
}
comma4 {
  var %a, %b = $regsub($ticks,$$1,/\G([+-]?\d+?)(?=(?:\d{3})++(?=\.\d++$|$))/g,\1 $+ $chr(44),%a)
  return %a
}
comma5 {
  if ($1 !isnum) return
  tokenize 46 $1
  return $+($iif(-* iswm $1,-),$bytes($abs($1),b),$iif($0 == 2,.),$2)
}

comma6 {
  return $bytes($1,bd)
}


As for removing commas, $remove(text,$chr(44)) removes all cases of $chr(44) in text. Text would likely be $1- for your case.

Thanks.

-Neal.

Re: Formatting a number [Re: LostShadow] #182038 03/08/07 06:18 PM
Joined: Dec 2002
Posts: 3,547
S
SladeKraven Offline
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 3,547
Quote:

Thanks to RusselB for giving me another $comma() identifier.


See most people usually collect stamps and coins and then there's you who collect comma aliases. Way to be unique. grin

Re: Formatting a number [Re: afterlife123] #182039 03/08/07 06:20 PM
Joined: Oct 2004
Posts: 8,327
Riamus2 Offline
Hoopy frood
Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,327
As shown, those are possible aliases to do what you need. Of course, if you want to do it without an alias (it's actually shorter code anyhow), you can just do:

If you're using decimals:
$bytes(number_without_commas,bd)

If you're not using decimals:
$bytes(number_without_commas,b)

To remove commas:
$remove(number_with_commas,$chr(44))

Obviously, you'd put either a variable or a $1 (or similar $N identifier) into where those numbers are.

Examples:
%num = 3246357

//echo -a $bytes(%num,b)
3,246,357

%num = 324754.45

//echo -a $bytes(%num,bd)
324,754.45

%num = 45,875,245

//echo -a $remove(%num,$chr(44))
45875245


Invision Support
#Invision on irc.irchighway.net
Re: Formatting a number [Re: Riamus2] #182060 04/08/07 12:01 AM
Joined: Aug 2004
Posts: 7,252
R
RusselB Offline
Hoopy frood
Offline
Hoopy frood
R
Joined: Aug 2004
Posts: 7,252
One that I like using is

Code:
return $iif($remove($1-,$chr(32),$chr(44)) isnum,$bytes($v1,bd),$1- isn't recognized as a number)