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 ".")
Format numeric presentation
-
- Posts: 4326
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: Format numeric presentation
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)
}
Re: Format numeric presentation
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
Re: Format numeric presentation
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 .:
The example from Filipeador .:
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
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
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
-
- Posts: 4326
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: Format numeric presentation
This is all not difficult, just adding another RegEx. My interest was to find one-string solution.
Re: Format numeric presentation
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
}
-
- Posts: 4326
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: Format numeric presentation
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
}
Re: Format numeric presentation
1 234,123 45
no, no, thats gulag worthy, im not changing it
no, no, thats gulag worthy, im not changing it
-
- Posts: 4326
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: Format numeric presentation
Why? If you separate integers before a comma, why not separate them after a comma?
Re: Format numeric presentation
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)
}
-
- Posts: 4326
- Joined: 29 Mar 2015, 09:41
- Contact:
-
- Posts: 4326
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: Format numeric presentation
Try: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
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)
}
Re: Format numeric presentation
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?
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?
-
- Posts: 4326
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: Format numeric presentation
I think, you could experiment on your own. To get rid of spaces and replace commas with points you can use StrReplace().
Who is online
Users browsing this forum: No registered users and 335 guests