|
Joined: Dec 2002
Posts: 68
Babel fish
|
OP
Babel fish
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.
|
|
|
|
Joined: Jan 2003
Posts: 44
Ameglian cow
|
Ameglian cow
Joined: Jan 2003
Posts: 44 |
can you post the solution anyways?
as it would help others wanting to learn.
thanks.
|
|
|
|
Joined: Jan 2003
Posts: 2,523
Hoopy frood
|
Hoopy frood
Joined: Jan 2003
Posts: 2,523 |
/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
|
|
|
|
Joined: Mar 2003
Posts: 32
Ameglian cow
|
Ameglian cow
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. Usage: either /commas 1234 or $commas(1234) in a script.
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.
|
|
|
|
Joined: Dec 2002
Posts: 3,138
Hoopy frood
|
Hoopy frood
Joined: Dec 2002
Posts: 3,138 |
The $bytes example querty posted works for an infinite length number...
|
|
|
|
Joined: Mar 2003
Posts: 32
Ameglian cow
|
Ameglian cow
Joined: Mar 2003
Posts: 32 |
And if you wanted to keep decimal places, the best way would probably be to do something like:
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.
|
|
|
|
Joined: Mar 2003
Posts: 32
Ameglian cow
|
Ameglian cow
Joined: Mar 2003
Posts: 32 |
Yeah, but his doesn't work with decimals now does it :P
|
|
|
|
Joined: Jan 2003
Posts: 2,523
Hoopy frood
|
Hoopy frood
Joined: Jan 2003
Posts: 2,523 |
alias commas return $bytes($1,b) $+ $mid($1,$pos($1,.)) or even shorter: 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
|
|
|
|
Joined: Mar 2003
Posts: 32
Ameglian cow
|
Ameglian cow
Joined: Mar 2003
Posts: 32 |
that or
alias commas return $bytes($1,b) $+ $right($calc($1 - $int($1)),-1)
|
|
|
|
Joined: Jan 2003
Posts: 2,523
Hoopy frood
|
Hoopy frood
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
|
|
|
|
Joined: Feb 2004
Posts: 2,019
Hoopy frood
|
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.
|
|
|
|
|