Some currency formatting regexes for v2

Put simple Tips and Tricks that are not entire Tutorials in this forum
burque505
Posts: 1747
Joined: 22 Jan 2017, 19:37

Some currency formatting regexes for v2

28 Jan 2023, 19:10

I always struggle with regular expressions - maybe I'm not alone. :D

Here are a few regexes to format currency in v2 (US only at the moment, apologies).

Code: Select all

curr := 1000489999.8
newcurr := RegExReplace(curr, "(\d)(?=(\d{3})+(?!\d))", "$1,")

MsgBox(newcurr "`n" "$" newcurr, "Does not ensure two decimal places")

num := 1234567
formattedNumber := RegExReplace(Format("{:.2f}", num), "(\d)(?=(\d{3})+(?!\d))", "$1,")

MsgBox(num "`n" "$" . formattedNumber, "Ensure two decimal places")

num2 := -1234567
formattedNumber2 := (num2 < 0 ? "-" : "") . RegExReplace(Format("{:.2f}", Abs(num2)), "(\d)(?=(\d{3})+(?!\d))", "$1,")

MsgBox(num2 "`n" "$" . formattedNumber2, "Not my favorite ...")

num3 := -12345.88
formattedNumber3 := (num3 < 0 ? "(" : "") . "$" . RegExReplace(Format("{:.2f}", Abs(num3)), "(\d)(?=(\d{3})+(?!\d))", "$1,") . (num3 < 0 ? ")" : "")

MsgBox(num3 "`n" formattedNumber3, "Enclosed in parens")

num4 := -12345.88
formattedNumber4 := (num4 < 0 ? "-" : "") . "$" . RegExReplace(Format("{:.2f}", Abs(num4)), "(\d)(?=(\d{3})+(?!\d))", "$1,")

MsgBox(num4 "`n" formattedNumber4, "Minus sign before")

MsgBox(-12345.88, "Was -12345.88")

MsgBox(12345.88, "Positive 12345.88")

MsgBox(Format("{:.2f}", -12345.67), "Using 'Format' on -12345.67")
(N.B.: The next-to-last two message boxes demonstrate that floats in a MsgBox appear to be formatted incorrectly. Bug? Feature? The last MsgBox is my workaround)

Regards,
burque505
iPhilip
Posts: 853
Joined: 02 Oct 2013, 12:21

Re: Some currency formatting regexes for v2

01 Feb 2023, 15:10

@burque505, You might also want to experiment with the two functions below.

Code: Select all

GetCurrencyFormatEx(Value, LocaleName := 0) {  ; LOCALE_NAME_USER_DEFAULT = 0
   LocaleType := LocaleName = 0 ? "Ptr" : "Str"
   if (NoChars := DllCall("GetCurrencyFormatEx", LocaleType, LocaleName, "UInt", 0, "Str", Value, "Ptr", 0, "Ptr", 0, "Int", 0, "Int"))
   && DllCall("GetCurrencyFormatEx", LocaleType, LocaleName, "UInt", 0, "Str", Value, "Ptr", 0, "Ptr", Currency := Buffer(2 * NoChars), "Int", NoChars, "Int")
      return StrGet(Currency)
}

GetNumberFormatEx(Value, LocaleName := 0) {  ; LOCALE_NAME_USER_DEFAULT = 0
   LocaleType := LocaleName = 0 ? "Ptr" : "Str"
   if (NoChars := DllCall("GetNumberFormatEx", LocaleType, LocaleName, "UInt", 0, "Str", Value, "Ptr", 0, "Ptr", 0, "Int", 0, "Int"))
   && DllCall("GetNumberFormatEx", LocaleType, LocaleName, "UInt", 0, "Str", Value, "Ptr", 0, "Ptr", Number := Buffer(2 * NoChars), "Int", NoChars, "Int")
      return StrGet(Number)
}
Cheers!
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)
burque505
Posts: 1747
Joined: 22 Jan 2017, 19:37

Re: Some currency formatting regexes for v2

01 Feb 2023, 15:20

@iPhilip, thanks for the tip! I'll do that.
Regards,
burque505
User avatar
jNizM
Posts: 3201
Joined: 30 Sep 2013, 01:33
Contact:

Re: Some currency formatting regexes for v2

02 Feb 2023, 02:35

iPhilip wrote:
01 Feb 2023, 15:10
alt: viewtopic.php?f=83&t=89720 -> see Strings
[AHK] v2.0.18 | [WIN] 11 Pro (23H2) | [GitHub] Profile

Return to “Tips and Tricks (v2)”

Who is online

Users browsing this forum: No registered users and 7 guests