No matter how hard I tried, I could not solve my desire, without to mark if the input value has decimals (
) or not.
Code: Select all
#SingleInstance force
; strings := ["123.1292", "1.000,62", "1,000.64", "1 000,45", "1000", "1,000", "200,45", "200,00", "10.0"]
strings := ["0.96", "1.000,42", "1,000.68", "1 000,50", "1000", "1,000", "200,45", "200,00", "10.0", "123.4321", "1 000 000,45"]
For index, currency in strings
{ text1 .= currency " => " currencyToDecimal(currency,",", 4, "n") "`n"
text2 .= currency " => " currencyToDecimal(currency,".", 2, "y") "`n"
}
MsgBox ,, %A_ScriptName% - Rad %A_LineNumber%, %text1% `n`n %text2%
ExitApp
currencyToDecimal(currency, decSign := ".", dec := 2, decExist := "y")
{ ; Version 04 - 16 January 2021 with help by @mikeyww
; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=81983
; @mikeyww on 11 October 2020, revised 12 October 2020 @Albireo
;
; Manages different currency structures (eg. 1.000,42 - 1,000.68 - 1000 - 1 000 000)
;
; currency - The number structure to be processed
; decsign - The decimal (. or ,) that should be present in the result
; dec - How many decimals should it be in the result
; decExist (y/n) - If the source (currency) has decimals (or not)
;
if !(decExist = "y")
currency := currency ".0"
currency := Trim(currency)
RegExMatch(currency, ".*[.,]\K\d{1,2}$", cents)
If (cents = "")
currency .= ".", cents := "0"
RegExMatch(currency, ".+(?=[.,])", dollars)
aDollar := Round(RegExReplace(dollars, "[ .,]") "." cents, dec)
aDollar := strReplace(aDollar, ".", decSign)
Return % aDollar
}