| View previous topic :: View next topic |
| Author |
Message |
infogulch
Joined: 27 Mar 2008 Posts: 649
|
Posted: Thu Feb 04, 2010 5:49 pm Post subject: Add Thousands Separator |
|
|
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 |
|
 |
sinkfaze
Joined: 18 Mar 2008 Posts: 5043 Location: the tunnel(?=light)
|
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4710 Location: Boulder, CO
|
Posted: Thu Feb 04, 2010 6:41 pm Post subject: |
|
|
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 |
|
 |
infogulch
Joined: 27 Mar 2008 Posts: 649
|
Posted: Thu Feb 04, 2010 10:03 pm Post subject: |
|
|
Laszlo: That's a good idea, I'll change it.
Thanks guys.  _________________ Scripts - License |
|
| Back to top |
|
 |
infogulch
Joined: 27 Mar 2008 Posts: 649
|
Posted: Thu Mar 04, 2010 6:59 pm Post subject: |
|
|
In response to the need shown in this thread, I decided to add an optional param for specifying your own seperator.
Updated.  _________________ Scripts - License |
|
| Back to top |
|
 |
Delusion
Joined: 16 Jul 2008 Posts: 210 Location: Greece/Rhodos
|
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4710 Location: Boulder, CO
|
Posted: Thu Mar 04, 2010 8:07 pm Post subject: |
|
|
| 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 |
|
 |
TheGood
Joined: 30 Jul 2007 Posts: 580
|
Posted: Thu Mar 04, 2010 11:28 pm Post subject: |
|
|
| 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 |
|
 |
Tuncay
Joined: 07 Nov 2006 Posts: 1886 Location: Germany
|
Posted: Thu Mar 11, 2010 9:20 pm Post subject: |
|
|
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!  _________________ {1:"ahkstdlib", 2:"my libs", 3:"my apps", 4:"my license"}
--> Don't feed the troll! <-- |
|
| Back to top |
|
 |
infogulch
Joined: 27 Mar 2008 Posts: 649
|
Posted: Tue Mar 16, 2010 9:17 pm Post subject: |
|
|
| 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 |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4710 Location: Boulder, CO
|
Posted: Tue Mar 16, 2010 11:40 pm Post subject: |
|
|
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 |
|
 |
infogulch
Joined: 27 Mar 2008 Posts: 649
|
Posted: Wed Mar 17, 2010 12:16 am Post subject: |
|
|
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 |
|
| Back to top |
|
 |
Zod
Joined: 06 Jul 2011 Posts: 214 Location: Looking over my domain
|
Posted: Fri Sep 23, 2011 4:19 pm Post subject: |
|
|
Wonder:
would it be possible to strip word characters easily? _________________
Stolen from SKAN |
|
| Back to top |
|
 |
infogulch
Joined: 27 Mar 2008 Posts: 649
|
Posted: Fri Sep 23, 2011 5:33 pm Post subject: |
|
|
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 |
|
 |
Zod
Joined: 06 Jul 2011 Posts: 214 Location: Looking over my domain
|
Posted: Fri Sep 23, 2011 6:22 pm Post subject: |
|
|
yea was just hoping for one step instead of 2 _________________
Stolen from SKAN |
|
| Back to top |
|
 |
|