AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Add a space every 3rd digit?

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
adamrgolf



Joined: 28 Dec 2006
Posts: 440

PostPosted: Thu Mar 04, 2010 2:03 pm    Post subject: Add a space every 3rd digit? Reply with quote

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
View user's profile Send private message
adamrgolf



Joined: 28 Dec 2006
Posts: 440

PostPosted: Thu Mar 04, 2010 2:06 pm    Post subject: Reply with quote

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
View user's profile Send private message
answer4u
Guest





PostPosted: Thu Mar 04, 2010 2:16 pm    Post subject: Reply with quote

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

PostPosted: Thu Mar 04, 2010 2:17 pm    Post subject: Reply with quote

Nice, answer4u!
Back to top
View user's profile Send private message
sinkfaze



Joined: 18 Mar 2008
Posts: 5043
Location: the tunnel(?=light)

PostPosted: Thu Mar 04, 2010 2:43 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail
adamrgolf



Joined: 28 Dec 2006
Posts: 440

PostPosted: Thu Mar 04, 2010 2:58 pm    Post subject: Reply with quote

Thanks, sinkfaze!
Back to top
View user's profile Send private message
infogulch



Joined: 27 Mar 2008
Posts: 649

PostPosted: Thu Mar 04, 2010 7:01 pm    Post subject: Reply with quote

FYI I updated ThousandsSep() with a user-defined separator param to cover instances like this.
_________________
Scripts - License
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group