AutoHotkey Community

It is currently May 27th, 2012, 2:14 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 15 posts ] 
Author Message
 Post subject: Add Thousands Separator
PostPosted: February 4th, 2010, 6:49 pm 
Offline

Joined: March 27th, 2008, 2:14 pm
Posts: 700
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 March 17th, 2010, 1:02 am, edited 4 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 4th, 2010, 7:13 pm 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5480
Location: the tunnel(?=light)
Very nice!

_________________
Image
Try Quick Search for Autohotkey or see the tutorial for newbies.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 4th, 2010, 7:41 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
Cool! Maybe the regex can be simplified a bit, keeping the conditional idea:
Code:
RegExReplace(x, "(?(?<=\.)(*COMMIT)(*FAIL))\d(?=(\d{3})+(\D|$))", "$0,")


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 4th, 2010, 11:03 pm 
Offline

Joined: March 27th, 2008, 2:14 pm
Posts: 700
Laszlo: That's a good idea, I'll change it.

Thanks guys. :)

_________________
Scripts - License


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 4th, 2010, 7:59 pm 
Offline

Joined: March 27th, 2008, 2:14 pm
Posts: 700
In response to the need shown in this thread, I decided to add an optional param for specifying your own seperator.

Updated. :)

_________________
Scripts - License


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 4th, 2010, 9:04 pm 
Offline
User avatar

Joined: July 17th, 2008, 12:58 am
Posts: 270
great! thank you very much for this

_________________
QuickSubs | Popcorn Movie Db
All my scripts are just in AutoHotkey v1.0.48.05


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 4th, 2010, 9:07 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 5th, 2010, 12:28 am 
Offline

Joined: July 30th, 2007, 11:32 pm
Posts: 581
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 11th, 2010, 10:20 pm 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
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! :D

_________________
{1:"ahkstdlib", 2:"my libs", 3:"my apps", 4:"my license"}
--> Don't feed the troll! <--


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 16th, 2010, 10:17 pm 
Offline

Joined: March 27th, 2008, 2:14 pm
Posts: 700
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 17th, 2010, 12:40 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 17th, 2010, 1:16 am 
Offline

Joined: March 27th, 2008, 2:14 pm
Posts: 700
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. :)

_________________
Scripts - License


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 23rd, 2011, 5:19 pm 
Offline

Joined: July 6th, 2011, 5:37 pm
Posts: 214
Location: Looking over my domain
Wonder:

would it be possible to strip word characters easily?

_________________
Image Stolen from SKAN


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 23rd, 2011, 6:33 pm 
Offline

Joined: March 27th, 2008, 2:14 pm
Posts: 700
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 23rd, 2011, 7:22 pm 
Offline

Joined: July 6th, 2011, 5:37 pm
Posts: 214
Location: Looking over my domain
yea was just hoping for one step instead of 2

_________________
Image Stolen from SKAN


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 15 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: MSN [Bot], notsoobvious and 8 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group