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 Thousands Separator

 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
infogulch



Joined: 27 Mar 2008
Posts: 649

PostPosted: Thu Feb 04, 2010 5:49 pm    Post subject: Add Thousands Separator Reply with quote

Here is a simple one-liner regex/function that add thousands separators to the correct spots in any decimal number with no loops, no recursion, and no callouts:

You can specify your own group separator either when you call it or you can modify the value in the function definition to match your locale.

Code:
ThousandsSep(x, s=",") {
   return RegExReplace(x, "\G\d+?(?=(\d{3})+(?:\D|$))", "$0" s)
}


And here is the test code I used for validating it, and the old version:
Code:
MsgBox % ""
   . "`n" ThousandsSep(1)
   . "`n" ThousandsSep(12)
   . "`n" ThousandsSep(123)
   . "`n" ThousandsSep(1234)
   . "`n" ThousandsSep(12345)
   . "`n" ThousandsSep(123456)
   . "`n" ThousandsSep(1234567)
   . "`n" ThousandsSep(12345678)
   . "`n" ThousandsSep(123456789)
   . "`n" ThousandsSep(1234567890)
   . "`n" ThousandsSep(1.1)
   . "`n" ThousandsSep(12.12)
   . "`n" ThousandsSep(123.123)
   . "`n" ThousandsSep(1234.1234)
   . "`n" ThousandsSep(12345.12345)
   . "`n" ThousandsSep(123456.123456)
   . "`n" ThousandsSep(1234567.1234567)
   . "`n" ThousandsSep(12345678.12345678)
   . "`n" ThousandsSep(123456789.123456789)
   . "`n" ThousandsSep(1234567890.1234567890)
; old version:      RegExReplace(x, "(?(?<=\" d ")(*COMMIT)(*FAIL))\d(?=(\d{3})+(\D|$))", "$0" g)
; old old version: RegExReplace(x, "(?(?<=\.)(*COMMIT)(*FAIL))(?<=\d)(\d{3})(?=(?:\d{3})*+(?:$|\.))", ",$1")

_________________
Scripts - License


Last edited by infogulch on Wed Mar 17, 2010 12:02 am; edited 4 times in total
Back to top
View user's profile Send private message
sinkfaze



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

PostPosted: Thu Feb 04, 2010 6:13 pm    Post subject: Reply with quote

Very nice!
_________________
Try Quick Search for Autohotkey or see the tutorial for newbies.
Back to top
View user's profile Send private message Send e-mail
Laszlo



Joined: 14 Feb 2005
Posts: 4710
Location: Boulder, CO

PostPosted: Thu Feb 04, 2010 6:41 pm    Post subject: Reply with quote

Cool! Maybe the regex can be simplified a bit, keeping the conditional idea:
Code:
RegExReplace(x, "(?(?<=\.)(*COMMIT)(*FAIL))\d(?=(\d{3})+(\D|$))", "$0,")
Back to top
View user's profile Send private message
infogulch



Joined: 27 Mar 2008
Posts: 649

PostPosted: Thu Feb 04, 2010 10:03 pm    Post subject: Reply with quote

Laszlo: That's a good idea, I'll change it.

Thanks guys. Smile
_________________
Scripts - License
Back to top
View user's profile Send private message
infogulch



Joined: 27 Mar 2008
Posts: 649

PostPosted: Thu Mar 04, 2010 6:59 pm    Post subject: Reply with quote

In response to the need shown in this thread, I decided to add an optional param for specifying your own seperator.

Updated. Smile
_________________
Scripts - License
Back to top
View user's profile Send private message
Delusion



Joined: 16 Jul 2008
Posts: 210
Location: Greece/Rhodos

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

great! thank you very much for this
_________________
Popcorn Movie Db
Simple Apnea Trainer
Back to top
View user's profile Send private message Visit poster's website
Laszlo



Joined: 14 Feb 2005
Posts: 4710
Location: Boulder, CO

PostPosted: Thu Mar 04, 2010 8:07 pm    Post subject: Reply with quote

infogulch wrote:
specifying your own seperator...
Maybe specifying the decimal separator (period or comma) would be helpful, too. It could default to the one specified by the user's locale, and then the default group separator might be the other character.
Back to top
View user's profile Send private message
TheGood



Joined: 30 Jul 2007
Posts: 580

PostPosted: Thu Mar 04, 2010 11:28 pm    Post subject: Reply with quote

Very elegant! Can you explain how the COMMIT and FAIL verbs work? I had a look at the PCRE docs but still can't wrap my head around it.
Back to top
View user's profile Send private message Visit poster's website
Tuncay



Joined: 07 Nov 2006
Posts: 1886
Location: Germany

PostPosted: Thu Mar 11, 2010 9:20 pm    Post subject: Reply with quote

Thx for the function. I have also made a solution, but that function is gone away. How? Harddisk crash!!
Btw, very very elegant. I name you to king Thousand! Very Happy
_________________
{1:"ahkstdlib", 2:"my libs", 3:"my apps", 4:"my license"}
--> Don't feed the troll! <--
Back to top
View user's profile Send private message Send e-mail Visit poster's website
infogulch



Joined: 27 Mar 2008
Posts: 649

PostPosted: Tue Mar 16, 2010 9:17 pm    Post subject: Reply with quote

Laszlo wrote:
Maybe specifying the decimal separator (period or comma) would be helpful, too.
Ah, good point! So I added another param to the function above.

However, I don't know how one could get the separator characters based on the user's current locale without a bunch of complexity. I mentioned that the params could be modified by the user.

TheGood: COMMIT and FAIL has to do with the flow of running a regex on text. To compare it to a loop, a plain FAIL would be like Continue, but COMMIT FAIL would be like a Break. The end result in this regex is to completely quit the regex and return once it passes a decimal point, since you don't want it adding separators after the decimal point. (e.g. 12.123,456,789 would be bad)
_________________
Scripts - License
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4710
Location: Boulder, CO

PostPosted: Tue Mar 16, 2010 11:40 pm    Post subject: Reply with quote

At least in Win7 you can get the decimal and thousand separator of the user's locale, from the registry:
Code:
RegRead d, HKEY_CURRENT_USER ,Control Panel\International, sDecimal
RegRead t, HKEY_CURRENT_USER ,Control Panel\International, sThousand
Back to top
View user's profile Send private message
infogulch



Joined: 27 Mar 2008
Posts: 649

PostPosted: Wed Mar 17, 2010 12:16 am    Post subject: Reply with quote

Well I asked in irc://freenode.net/#regex and Billiard came up with a similar solution for this that doesn't require specifying the decimal character. Find it in the first post.

Instead of using COMMIT FAIL, it uses the \G anchor (which I didn't know existed :S). The \G anchor forces it to match either at the very beginning of the string or at the end of the previous match. Since a decimal separator won't match, it forces a fail at that point, regardless of what kind of decimal separator it is.

Laszlo: I figured one could easily get the values from the registry, but I'm saying it's out of the scope of this function. Thanks for the info though; now if someone does want to make a locale-specific ThousandsSep function, they have the resources right here to do it. Smile
_________________
Scripts - License
Back to top
View user's profile Send private message
Zod



Joined: 06 Jul 2011
Posts: 214
Location: Looking over my domain

PostPosted: Fri Sep 23, 2011 4:19 pm    Post subject: Reply with quote

Wonder:

would it be possible to strip word characters easily?
_________________
Stolen from SKAN
Back to top
View user's profile Send private message
infogulch



Joined: 27 Mar 2008
Posts: 649

PostPosted: Fri Sep 23, 2011 5:33 pm    Post subject: Reply with quote

Zod: by "strip word characters" I assume you're talking about non-numeric characters in the string with the number.

In that case I would instead suggest that you extract out all the numbers, add thousands separators to them with this function, then replace them in the original string or some similar combination.
_________________
Scripts - License
Back to top
View user's profile Send private message
Zod



Joined: 06 Jul 2011
Posts: 214
Location: Looking over my domain

PostPosted: Fri Sep 23, 2011 6:22 pm    Post subject: Reply with quote

yea was just hoping for one step instead of 2
_________________
Stolen from SKAN
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions 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