Format numeric presentation

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Albireo
Posts: 1753
Joined: 16 Oct 2013, 13:53

Format numeric presentation

15 Feb 2019, 05:25

Suppose I have numbers larger than eg. 999. Then it can be easier to read the numerals, if the number is divided into groups.
Something like this (in Sweden) 1234,50 = 1.234,50 or 1 234,50 An another example is 1234567 = 1 234 567
I dont know how to use String := Format(FormatStr [, Values...]) instead of SetFormat
Is it possible? (Note the decimal comma in Sweden "10,50" instead of decimal point "10.50")
(If it's difficult or impossible to handle Swedish decimal comma in AHK - it's ok with decimal point ".")
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Format numeric presentation

15 Feb 2019, 08:44

As I know, it is not possible using Format() but possible using Regex:

Code: Select all

num := "1234,50"
MsgBox, % SeparateThousends(num)

SeparateThousends(num, thousendsSepar := " ", decimalSepar := ",") {
   d := decimalSepar
   Return RegExReplace(num, "x) ^-?\d?\K\d(?=(\d{3})+(\" . d . "|$)) | (?1)(?!(\" . d . "|$))", "$0" . thousendsSepar)
}
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: Format numeric presentation

15 Feb 2019, 10:19

Code: Select all

Num := "1234,50"
Num := StrReplace( Num, ",", "." )
r := GetNumberFormat(Num, "sv-SE")
; https://stackoverflow.com/questions/2132348/what-does-char-160-mean-in-my-source-code
MsgBox % r "`n" RegExReplace(r,chr(160),".")

GetNumberFormat(Value, LocaleName)  ; WIN_V+
{
    ; LocaleName:
    ; https://docs.microsoft.com/es-es/windows/desktop/Intl/locale-names
    ; https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
    ; https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes
    local NumberStr := ""
    VarSetCapacity(NumberStr, 102, 0)
    DllCall("Kernel32.dll\GetNumberFormatEx", "UPtr", &LocaleName, "UInt", 0, "UPtr", &Value, "UPtr", 0, "Str", NumberStr, "Int", 50)
    return NumberStr
} ; https://docs.microsoft.com/es-es/windows/desktop/api/winnls/nf-winnls-getnumberformatex
https://www.autohotkey.com/boards/search.php?keywords=GetNumberFormat
Albireo
Posts: 1753
Joined: 16 Oct 2013, 13:53

Re: Format numeric presentation

15 Feb 2019, 15:52

Thanks!
The suggestions were pretty good (but still not perfect). The results and wishes are available as remarks in the code.
The example from teadrinker .:

Code: Select all

MsgBox ,, Row %A_LineNumber% -> %A_ScriptName%, % SeparateThousends("1234")	; => 1 234	(OK!)
MsgBox ,, Row %A_LineNumber% -> %A_ScriptName%, % SeparateThousends(".50")	; => .50  	(My wish 0,50 or 0,5)
MsgBox ,, Row %A_LineNumber% -> %A_ScriptName%, % SeparateThousends("12234.50")	; => 122 34.50  (My wish 12 234,50)
MsgBox ,, Row %A_LineNumber% -> %A_ScriptName%, % SeparateThousends("12234,50")	; => 12 234,50  (OK!)

SeparateThousends(num, thousendsSepar := " ", decimalSepar := ",") {
   d := decimalSepar
   Return RegExReplace(num, "x) ^-?\d?\K\d(?=(\d{3})+(\" . d . "|$)) | (?1)(?!(\" . d . "|$))", "$0" . thousendsSepar)
}

The example from Filipeador .:

Code: Select all

MsgBox ,, Row %A_LineNumber% -> %A_ScriptName%, % GetNumberFormat("1234")	; => 1 234,00 (My wish 1 234)
MsgBox ,, Row %A_LineNumber% -> %A_ScriptName%, % GetNumberFormat(".50")	; => 0,50      (OK!)
MsgBox ,, Row %A_LineNumber% -> %A_ScriptName%, % GetNumberFormat("12234.50")	; => 12 234,50 (OK!)
MsgBox ,, Row %A_LineNumber% -> %A_ScriptName%, % GetNumberFormat("12234,50")	; => 12 234,50 (OK!)

GetNumberFormat(Value, LocaleName := "sv-SE")  ; WIN_V+
{	; LocaleName:
	; https://docs.microsoft.com/es-es/windows/desktop/Intl/locale-names
	; https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
	; https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes
	
	Local NumberStr := ""
	Value := StrReplace( Value, ",", "." )
	
	VarSetCapacity(NumberStr, 102, 0)
	DllCall("Kernel32.dll\GetNumberFormatEx", "UPtr", &LocaleName, "UInt", 0, "UPtr", &Value, "UPtr", 0, "Str", NumberStr, "Int", 50)
	Return NumberStr
} ; https://docs.microsoft.com/es-es/windows/desktop/api/winnls/nf-winnls-getnumberformatex
The goal is to handle numerical numbers as numbers in AHK (and here have all decimal number, the decimal point ".")

a) Layout
If the numerical numbers become larger (eg higher than 1000), it is easier to read the number 1 000 by the space (I don't need the 1,000 alternative - right now). If the number is properly formatted, it is possible to copy it directly to a cell in LibreOffice Calc (Swedish version) (eg. 12 234,59 or 12234,59)

b) Decimal numbers
I think that 0,5 or 0,50 (free number of decimals) is nicer than ,5.

c) Manage decimals
Most importantly, the result has the same number of decimals as the number to be handled.
eg. 43.543 => 43,543 or .5 => 0,5 or 123 => 123

d) Fixed decimals
Would be nice if it is easy and possible to determine how many decimals the result will get from the "format"-function.
eg. (with two fixed decimals) 43 => 43,00 or .5 => 0,50 or 1234567.543 => 1 234 567,54
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Format numeric presentation

15 Feb 2019, 16:53

This is all not difficult, just adding another RegEx. My interest was to find one-string solution. :)
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Format numeric presentation

16 Feb 2019, 07:33

Code: Select all

MsgBox ,, Row %A_LineNumber% -> %A_ScriptName%, % SeparateThousends("1234")	; => 1 234	(OK!)
MsgBox ,, Row %A_LineNumber% -> %A_ScriptName%, % SeparateThousends(".50")	; => .50  	(My wish 0,50 or 0,5)
MsgBox ,, Row %A_LineNumber% -> %A_ScriptName%, % SeparateThousends("12234.50")	; => 122 34.50  (My wish 12 234,50)
MsgBox ,, Row %A_LineNumber% -> %A_ScriptName%, % SeparateThousends("12234,50")	; => 12 234,50  (OK!)

SeparateThousends(num, thousendsSepar := " ", decimalSepar := ",") {
	num := StrReplace(num, ".", decimalSepar)

	if InStr(num, decimalSepar)
	{
		T := StrSplit(num, decimalSepar)
		num := T[1] ? T[1] : 0
		fraction := decimalSepar T[2]
	}

	return RegExReplace(num, "\d(?=(?:\d{3})+(?!\d))", "$0" thousendsSepar) fraction
}
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Format numeric presentation

16 Feb 2019, 08:00

swagfag wrote:

Code: Select all

MsgBox % SeparateThousends("1234.12345") ; => 1 234,12345 (My wish 1 234,123 45)

SeparateThousends(num, thousendsSepar := " ", decimalSepar := ",") {
   num := StrReplace(num, ".", decimalSepar)

   if InStr(num, decimalSepar)
   {
      T := StrSplit(num, decimalSepar)
      num := T[1] ? T[1] : 0
      fraction := decimalSepar T[2]
   }

   return RegExReplace(num, "\d(?=(?:\d{3})+(?!\d))", "$0" thousendsSepar) fraction
}
;)
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Format numeric presentation

16 Feb 2019, 08:24

1 234,123 45 :shock:
no, no, thats gulag worthy, im not changing it
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Format numeric presentation

16 Feb 2019, 08:48

Why? If you separate integers before a comma, why not separate them after a comma?
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Format numeric presentation

16 Feb 2019, 08:57

ehhhhhhhhhhh fiine, heres the lazy solution

Code: Select all

MsgBox % SeparateThousends("1234.12345") ; => 1 234,12345 (My wish 1 234,123 45)

SeparateThousends(num, thousendsSepar := " ", decimalSepar := ",") {
   num := StrReplace(num, ".", decimalSepar)

   if InStr(num, decimalSepar)
   {
      T := StrSplit(num, decimalSepar)
      num := T[1] ? T[1] : 0
      fraction := decimalSepar T[2]
   }

   return RegExReplace(num, "\d(?=(?:\d{3})+(?!\d))", "$0" thousendsSepar) RegExReplace(fraction, "\d(?=(?:\d{3})+(?!\d))", "$0" thousendsSepar)
}
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Format numeric presentation

16 Feb 2019, 11:55

Albireo wrote: a) Layout
If the numerical numbers become larger (eg higher than 1000), it is easier to read the number 1 000 by the space (I don't need the 1,000 alternative - right now). If the number is properly formatted, it is possible to copy it directly to a cell in LibreOffice Calc (Swedish version) (eg. 12 234,59 or 12234,59)

b) Decimal numbers
I think that 0,5 or 0,50 (free number of decimals) is nicer than ,5.

c) Manage decimals
Most importantly, the result has the same number of decimals as the number to be handled.
eg. 43.543 => 43,543 or .5 => 0,5 or 123 => 123

d) Fixed decimals
Would be nice if it is easy and possible to determine how many decimals the result will get from the "format"-function.
eg. (with two fixed decimals) 43 => 43,00 or .5 => 0,50 or 1234567.543 => 1 234 567,54
Try:

Code: Select all

MsgBox % SeparateThousends("1234567")
MsgBox % SeparateThousends("1234567,1234", 2)
MsgBox % SeparateThousends(".5", 2)
MsgBox % SeparateThousends("12345.5", 2)
MsgBox % SeparateThousends("12345,567", 2)

SeparateThousends(num, decimalCount := "", thousendsSepar := " ", decimalSepar := ",") {
   if (decimalCount != "") {
      num := StrReplace(num, ",", ".")
      num := Round(num, decimalCount)
   }
   num := StrReplace(num, ".", d := decimalSepar)
   Return RegExReplace(num, "x) ^-?\d?\K\d(?=(\d{3})+(\" . d . "|$)) | (?1)(?!(\" . d . "|$))", "$0" . thousendsSepar)
}
Albireo
Posts: 1753
Joined: 16 Oct 2013, 13:53

Re: Format numeric presentation

18 Feb 2019, 18:13

Thank you!
Nice solutions! But… (I hope I can explain)

I have several places where numerical combinations should be managed with different conditions.

1.) In AHK
If I want to handle numbers in AHK, should it only be numbers with decimal point"." and no spaces or other marks for e.g. thousand numbers (12345.4321)


2.) In GUI- / MsgBox- window
Is it often more easy to read the numbers, if e.g. thousand numbers have a space (eg. 12 345) and in Sweden we have decimal comma"," (eg. 12 345,43)

3.) From AHK to LibreOffice Calc
Sometimes I want to transfer a value (eg. 12345.54321) from AHK, to a particular cell in LibreOffice Calc (Swedish).
Then the number must have the following format (with a freely number of decimals)
eg. 1 12345,4321 or 12 345,4321

The final proposal seems to work so far (except for the space in the decimal part).
Choosing the number of decimals seem to work as desired.
The next challenge in the same theme might be more difficult?

4.) To AHK from LibreOffice Calc
It happens that I want to use a number, contained in a cell in LibreOffice Calc.
12345,4321 or 12 345,4321 (and more difficult 12 345,43 kr)
I want to format these numbers, so it can be handled by AHK (eg. 12345.4321)

a) Is it possible?
b) Whith the same function?
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Format numeric presentation

20 Feb 2019, 10:25

I think, you could experiment on your own. To get rid of spaces and replace commas with points you can use StrReplace().

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 335 guests