| View previous topic :: View next topic |
| Author |
Message |
adamrgolf
Joined: 28 Dec 2006 Posts: 440
|
Posted: Thu Mar 04, 2010 2:03 pm Post subject: Add a space every 3rd digit? |
|
|
I want to transform a string of numbers into a string with numbers separated in sets of three, where a comma would normally be for readability.
Example:
1234 would be 1 234
12345 would be 12 345
123456 would be 123 456
1234567 would be 1 234 567
and so on...
I believe this is possible with RegExReplace (possibly) or perhaps a parsing loop, but my attempt fails in that it seems to work in reverse:
| Code: | #SingleInstance,Force
Num=1234567
h=0
t=
Loop,parse,Num
{
If ! Mod(h,3)
t = %t% %A_LoopField%
Else
t = %t%%A_LoopField%
h++
}
msgbox %Num%`n%t% |
Can anyone offer any help?
Thanks! |
|
| Back to top |
|
 |
adamrgolf
Joined: 28 Dec 2006 Posts: 440
|
Posted: Thu Mar 04, 2010 2:06 pm Post subject: |
|
|
This seems to do it, although I'm sure there is a shorter way:
| Code: | #SingleInstance,Force
Num=1234
h=0
t=
Loop,parse,Num
t = %A_LoopField%%t%
Num := t
t=
Loop,parse,Num
{
If ! Mod(h,3)
t = %A_LoopField% %t%
Else
t = %A_LoopField%%t%
h++
}
msgbox %Num%`n%t% |
|
|
| Back to top |
|
 |
answer4u Guest
|
Posted: Thu Mar 04, 2010 2:16 pm Post subject: |
|
|
Here's a quick example:string := "12345678910"
| Code: | While, pos := RegExMatch( string, "\d{4}(\s|$)" )
string := SubStr( string, 1, pos ) " " SubStr( string, pos+1 )
MsgBox, %string% |
|
|
| Back to top |
|
 |
adamrgolf
Joined: 28 Dec 2006 Posts: 440
|
Posted: Thu Mar 04, 2010 2:17 pm Post subject: |
|
|
| Nice, answer4u! |
|
| Back to top |
|
 |
sinkfaze
Joined: 18 Mar 2008 Posts: 5043 Location: the tunnel(?=light)
|
Posted: Thu Mar 04, 2010 2:43 pm Post subject: |
|
|
Adapting this technique would probably be the easiest way:
| Code: | MsgBox % ThousandsSep(1234)
. "`n" ThousandsSep(12345)
. "`n" ThousandsSep(123456)
. "`n" ThousandsSep(1234567)
ThousandsSep(x) {
return RegExReplace(x, "(?(?<=\.)(*COMMIT)(*FAIL))\d(?=(\d{3})+(\D|$))", "$0 ")
} |
_________________ Try Quick Search for Autohotkey or see the tutorial for newbies. |
|
| Back to top |
|
 |
adamrgolf
Joined: 28 Dec 2006 Posts: 440
|
Posted: Thu Mar 04, 2010 2:58 pm Post subject: |
|
|
| Thanks, sinkfaze! |
|
| Back to top |
|
 |
infogulch
Joined: 27 Mar 2008 Posts: 649
|
Posted: Thu Mar 04, 2010 7:01 pm Post subject: |
|
|
FYI I updated ThousandsSep() with a user-defined separator param to cover instances like this. _________________ Scripts - License |
|
| Back to top |
|
 |
|