WinAPI - GetXxxFormat Functions

Post your working scripts, libraries and tools
User avatar
jNizM
Posts: 2317
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

WinAPI - GetXxxFormat Functions

02 Nov 2015, 09:14

GetXxxFormat Functions (National Language Support Functions) (MSDN-Link)


- GetCurrencyFormat & GetCurrencyFormatEx (Formats a number string as a currency string)
- GetNumberFormat & GetNumberFormatEx (Formats a number string as a number string)
- GetDateFormat & GetDateFormatEx
- GetDurationFormat & GetDurationFormatEx
- GetTimeFormat & GetTimeFormatEx
[AHK] 1.1.28.02 x64 Unicode | [WIN] 10 Pro (Version 1803) x64 | [GitHub] Profile
Donations are appreciated if I could help you
User avatar
jNizM
Posts: 2317
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: WinAPI GetXxxFormat Functions

03 Nov 2015, 02:05

GetCurrencyFormat
Formats a number string as a currency string for a locale specified by identifier.

Code: [Select all] [Download] GeSHi © Codebox Plus

GetCurrencyFormat(VarIn, locale := 0x0400)
{
if (size := DllCall("GetCurrencyFormat", "uint", locale, "uint", 0, "ptr", &VarIn, "ptr", 0, "ptr", 0, "int", 0)) {
VarSetCapacity(buf, size << 1, 0)
if (DllCall("GetCurrencyFormat", "uint", locale, "uint", 0, "ptr", &VarIn, "ptr", 0, "str", buf, "int", size))
return buf
}
return false
}

example & output

Code: [Select all] [Download] GeSHi © Codebox Plus

MsgBox % GetCurrencyFormat(1149.99)                ; ==> 1.149,99 €        ( LANG_USER_DEFAULT | SUBLANG_DEFAULT    )
MsgBox % GetCurrencyFormat(1149.99, 0x0409) ; ==> $1,149.99 ( LANG_ENGLISH | SUBLANG_ENGLISH_US )
MsgBox % GetCurrencyFormat(1149.99, 0x0809) ; ==> £1,149.99 ( LANG_ENGLISH | SUBLANG_ENGLISH_UK )
MsgBox % GetCurrencyFormat(1149.99, 0x0407) ; ==> 1.149,99 € ( LANG_GERMAN | SUBLANG_GERMAN )

ref:
- GetCurrencyFormat function
- Language Identifier Constants and Strings



GetCurrencyFormatEx (Vista or later)
Formats a number string as a currency string for a locale specified by name.

Code: [Select all] [Download] GeSHi © Codebox Plus

GetCurrencyFormatEx(VarIn, locale := "!x-sys-default-locale")
{
if (size := DllCall("GetCurrencyFormatEx", "ptr", &locale, "uint", 0, "ptr", &VarIn, "ptr", 0, "ptr", 0, "int", 0)) {
VarSetCapacity(buf, size << 1, 0)
if (DllCall("GetCurrencyFormatEx", "ptr", &locale, "uint", 0, "ptr", &VarIn, "ptr", 0, "str", buf, "int", size))
return buf
}
return false
}

example & output

Code: [Select all] [Download] GeSHi © Codebox Plus

MsgBox % GetCurrencyFormatEx(1149.99)                 ; ==> 1.149,99 €        ( LANG_USER_DEFAULT | SUBLANG_DEFAULT    )
MsgBox % GetCurrencyFormatEx(1149.99, "en-US") ; ==> $1,149.99 ( LANG_ENGLISH | SUBLANG_ENGLISH_US )
MsgBox % GetCurrencyFormatEx(1149.99, "en-GB") ; ==> £1,149.99 ( LANG_ENGLISH | SUBLANG_ENGLISH_US )
MsgBox % GetCurrencyFormatEx(1149.99, "de-DE") ; ==> 1.149,99 € ( LANG_GERMAN | SUBLANG_GERMAN )

ref:
- GetCurrencyFormatEx function
- Locale Names
[AHK] 1.1.28.02 x64 Unicode | [WIN] 10 Pro (Version 1803) x64 | [GitHub] Profile
Donations are appreciated if I could help you
User avatar
jNizM
Posts: 2317
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: WinAPI GetXxxFormat Functions

03 Nov 2015, 02:05

GetNumberFormat
Formats a number string as a number string customized for a locale specified by identifier.

Code: [Select all] [Download] GeSHi © Codebox Plus

GetNumberFormat(VarIn, locale := 0x0400)
{
if (size := DllCall("GetNumberFormat", "uint", locale, "uint", 0, "ptr", &VarIn, "ptr", 0, "ptr", 0, "int", 0)) {
VarSetCapacity(buf, size << 1, 0)
if (DllCall("GetNumberFormat", "uint", locale, "uint", 0, "ptr", &VarIn, "ptr", 0, "str", buf, "int", size))
return buf
}
return false
}

example & output

Code: [Select all] [Download] GeSHi © Codebox Plus

MsgBox % GetNumberFormat(1149.99)                  ; ==> 1.149,99         ( LANG_USER_DEFAULT | SUBLANG_DEFAULT    )
MsgBox % GetNumberFormat(1149.99, 0x0409) ; ==> 1,149.99 ( LANG_ENGLISH | SUBLANG_ENGLISH_US )
MsgBox % GetNumberFormat(1149.99, 0x0809) ; ==> 1,149.99 ( LANG_ENGLISH | SUBLANG_ENGLISH_UK )
MsgBox % GetNumberFormat(1149.99, 0x0407) ; ==> 1.149,99 ( LANG_GERMAN | SUBLANG_GERMAN )

ref:
- GetNumberFormat function
- Language Identifier Constants and Strings



GetNumberFormatEx (Vista or later)
Formats a number string as a number string customized for a locale specified by name.

Code: [Select all] [Download] GeSHi © Codebox Plus

GetNumberFormatEx(VarIn, locale := "!x-sys-default-locale")
{
if (size := DllCall("GetNumberFormatEx", "ptr", &locale, "uint", 0, "ptr", &VarIn, "ptr", 0, "ptr", 0, "int", 0)) {
VarSetCapacity(buf, size << 1, 0)
if (DllCall("GetNumberFormatEx", "ptr", &locale, "uint", 0, "ptr", &VarIn, "ptr", 0, "str", buf, "int", size))
return buf
}
return false
}

example & output

Code: [Select all] [Download] GeSHi © Codebox Plus

MsgBox % GetNumberFormatEx(1149.99)                   ; ==> 1.149,99         ( LANG_USER_DEFAULT | SUBLANG_DEFAULT    )
MsgBox % GetNumberFormatEx(1149.99, "en-US") ; ==> 1,149.99 ( LANG_ENGLISH | SUBLANG_ENGLISH_US )
MsgBox % GetNumberFormatEx(1149.99, "en-GB") ; ==> 1,149.99 ( LANG_ENGLISH | SUBLANG_ENGLISH_US )
MsgBox % GetNumberFormatEx(1149.99, "de-DE") ; ==> 1.149,99 ( LANG_GERMAN | SUBLANG_GERMAN )

ref:
- GetNumberFormatEx function
- Locale Names
[AHK] 1.1.28.02 x64 Unicode | [WIN] 10 Pro (Version 1803) x64 | [GitHub] Profile
Donations are appreciated if I could help you
User avatar
jNizM
Posts: 2317
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: WinAPI GetXxxFormat Functions

03 Nov 2015, 02:05

*reserved for GetDateFormat & GetDateFormatEx
[AHK] 1.1.28.02 x64 Unicode | [WIN] 10 Pro (Version 1803) x64 | [GitHub] Profile
Donations are appreciated if I could help you
User avatar
jNizM
Posts: 2317
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: WinAPI GetXxxFormat Functions

03 Nov 2015, 02:16

*reserved for GetDurationFormat & GetDurationFormatEx
[AHK] 1.1.28.02 x64 Unicode | [WIN] 10 Pro (Version 1803) x64 | [GitHub] Profile
Donations are appreciated if I could help you
User avatar
jNizM
Posts: 2317
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: WinAPI GetXxxFormat Functions

03 Nov 2015, 03:10

*reserved for GetTimeFormat & GetTimeFormatEx
[AHK] 1.1.28.02 x64 Unicode | [WIN] 10 Pro (Version 1803) x64 | [GitHub] Profile
Donations are appreciated if I could help you
Albireo
Posts: 399
Joined: 16 Oct 2013, 13:53

Re: WinAPI - GetXxxFormat Functions

18 May 2016, 17:10

Thank You!
It works fine.

Do you still intend to supplement with "GetDateFormatEx" and "GetTimeFormatEx"?

//Jan
User avatar
jeeswg
Posts: 4305
Joined: 19 Dec 2016, 01:58
Location: UK

Re: WinAPI - GetXxxFormat Functions

12 Sep 2017, 02:50

@Albireo: jNizM did do a GetDateFormat example here:
Convert .NET DateTime ticks to AHK YYYYMMDDHH24MISS timestamp - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=8992&p=49935#p49935
I was looking forward to some date/time format examples! But when I got there, the cupboard was bare.

I was trying to find out if GetTimeFormat can handle milliseconds, the answer appears to be no. Although I have some date/time and milliseconds examples here:
jeeswg's documentation extension tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=33596

@jNizM: You're welcome to adapt any of these examples.

Another useful link:
Language Identifier Constants and Strings (Windows)
https://msdn.microsoft.com/en-us/library/windows/desktop/dd318693(v=vs.85).aspx

Here are some examples with GetDateFormat and GetTimeFormat:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

User avatar
jeeswg
Posts: 4305
Joined: 19 Dec 2016, 01:58
Location: UK

Re: WinAPI - GetXxxFormat Functions

18 Dec 2017, 11:57

It might be worth including this here. StrFormatByteSize64 to display a file size in a friendly format.

Code: [Select all] [Download] GeSHi © Codebox Plus

q:: ;friendly display file size
vText := ""
vSizeFile := 123456789
VarSetCapacity(vText, vSize := 260*2, 0) ;(no suggested size in MSDN)
DllCall("shlwapi\StrFormatByteSize64", Int64,vSizeFile, Str,vText, UInt,vSize, Ptr)
MsgBox, % vText ;117 MB
return

Link:
FormatBytes() - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=3567
[EDIT:] Changed from StrFormatByteSize to StrFormatByteSize64. StrFormatByteSize handles UInt, StrFormatByteSize64 handles Int64.
Last edited by jeeswg on 19 Dec 2017, 06:36, edited 1 time in total.
User avatar
jNizM
Posts: 2317
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: WinAPI - GetXxxFormat Functions

19 Dec 2017, 06:16

[AHK] 1.1.28.02 x64 Unicode | [WIN] 10 Pro (Version 1803) x64 | [GitHub] Profile
Donations are appreciated if I could help you
User avatar
Drugwash
Posts: 560
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania

Re: WinAPI - GetXxxFormat Functions

20 Dec 2017, 06:53

Regarding the above:
@ jNizM: At your github, the StrFormatByteSize64() function is still called StrFormatByteSizeEx(). Is that intended or just a copy/paste leftover?

Has anyone noticed the huge stupid change M$ did in these byte-formatting APIs?
StrFormatKBSize
StrFormatByteSizeA
StrFormatByteSizeW
StrFormatByteSize64
StrFormatByteSizeEx

Quote from MSDN:
In Windows 10, size is reported in base 10 rather than base 2. For example, 1 KB is 1000 bytes rather than 1024.


Imagine the confusion when any two people, one using Win10 and the other anything else, try to compare results of the same whatever code (AHK, C/C++/C#, etc) that's using those APIs.

Maybe M$ intend to make the BYTE 10 bits long instead of 8. Maybe they want to move everything to decimal base…

Forgot to say thank you for these GetXxxFormat functions, I needed them so many times in the past and never stumbled into these APIs. Or maybe I did and just forgot. Anyways, thank you jNizM! :thumbup:
I've deleted my CloudMe account because of GDPR - the now legal base for privacy invasion and data theft.
User avatar
jeeswg
Posts: 4305
Joined: 19 Dec 2016, 01:58
Location: UK

Re: WinAPI - GetXxxFormat Functions

20 Dec 2017, 07:05

@Drugwash: Yes, the changes are very confusing.

FormatBytes() - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=3567
No, the documentation is clearly wrong. On Windows 10.0.10586, SKAN's example code and Explorer itself both show 1GB as 1024*1024*1024 bytes.

StrFormatByteSize64 function (Windows)
https://msdn.microsoft.com/en-us/library/windows/desktop/bb759971(v=vs.85).aspx
StrFormatByteSize64 can be used for either ANSI or Unicode characters. However, while StrFormatByteSize64A can be called directly, StrFormatByteSize64W is not defined. When StrFormatByteSize64 is called with a Unicode value, StrFormatByteSizeW is used.
User avatar
Drugwash
Posts: 560
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania

Re: WinAPI - GetXxxFormat Functions

20 Dec 2017, 08:11

Ah, thanks, I was beating a dead horse. :oops:
Anyway, the documentation is even worse as it doesn't mention terabytes, petabytes, exabytes as possible result but only maximum gigabytes.

Second quote hides a catch: the automatic switch to StrFormatByteSizeW() is not done in AHK but only in C code.
Calling "shlwapi\StrFormatByteSize64" with either a Unicode or ANSI buffer returns zero but doesn't set A_LastError.

Well, that's about it, we're kinda off-topic here. :)
I've deleted my CloudMe account because of GDPR - the now legal base for privacy invasion and data theft.

Return to “Scripts and Functions”

Who is online

Users browsing this forum: dcard and 11 guests