Formatting numbers with thousands separators

Post your working scripts, libraries and tools for AHK v1.1 and older
JBensimon
Posts: 118
Joined: 19 Nov 2017, 11:19

Formatting numbers with thousands separators

03 Jan 2020, 17:40

Since Format() apparently doesn't have an option to add thousands separators (commas in my case) to its results, I use the function below to get the job done (after using Format() if necessary). It takes a single argument with optional negative sign and fractional part, calculates the number N of separators that will be required, generates appropriate RegEx and replacement patterns based on N (assumes numbers from -2^63 to (2^63-1), the 64-bit extremes), and applies them to the input. Is there a better way? (I chose a RegEx approach to avoid looping).

Code: Select all

ThouSep(X)
{
  N := (StrLen(Floor(Abs(X))) - 1) // 3
  Pat := "^(-?\d{1,3})" . SubStr("(\d{3})(\d{3})(\d{3})(\d{3})(\d{3})(\d{3})", 1, 7*N) . "(\.\d*)?$"
  Rep := "$1" . SubStr(",$2,$3,$4,$5,$6,$7", 1, 3*N) . "$" . N+2
  Return, RegExReplace(X, Pat, Rep)
}
Modify as needed for different thousands and decimal separators (or be ambitious and read them from the Registry :) ).

Update: See the first reply for a superior RegEx solution that uses a single generic pattern. Needs modification to handle negative numbers, but impressive nonetheless.

Update 2: I'm not going to feel embarrassed (much) for posting my function, but the alternative RegEx and WinAPI solutions offered below are clearly the better approaches. Thank you all!

Jacques.
Last edited by JBensimon on 07 Jan 2020, 09:50, edited 2 times in total.
User avatar
TheDewd
Posts: 1513
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Formatting numbers with thousands separators

06 Jan 2020, 09:36

http://autohotkey.com/board/topic/50019-add-thousands-separator/?p=312069

Code: Select all

MsgBox, % ThousandsSep("5555555555")

ThousandsSep(x, s=",") {
	return RegExReplace(x, "\G\d+?(?=(\d{3})+(?:\D|$))", "$0" s)
}
JBensimon
Posts: 118
Joined: 19 Nov 2017, 11:19

Re: Formatting numbers with thousands separators

06 Jan 2020, 09:43

Wow! Thanks, Dewd. I'll analyze the patterns later, but I already see I'm not familiar with the $0 reference (or forgot it if I ever was).

Jacques.
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Formatting numbers with thousands separators

07 Jan 2020, 03:04

[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
JBensimon
Posts: 118
Joined: 19 Nov 2017, 11:19

Re: Formatting numbers with thousands separators

07 Jan 2020, 06:38

jNizM wrote:
07 Jan 2020, 03:04
Or using WinAPI (GetNumberFormat / GetNumberFormatEx)
Another great solution! Thanks.
JBensimon
Posts: 118
Joined: 19 Nov 2017, 11:19

Re: Formatting numbers with thousands separators

07 Jan 2020, 09:43

JBensimon wrote:
06 Jan 2020, 09:43
I'll analyze the patterns later ...
Okay, I get it now. Using "\G" = "start of match" and "(?=" = look-ahead assertion is brilliant stuff!

JB

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: Descolada and 184 guests