Convert many currency format to decimals Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Convert many currency format to decimals

17 Jan 2021, 07:21

Perhaps you can explain more about your question and how you are assessing integer status.
Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

Re: Convert many currency format to decimals

17 Jan 2021, 11:05

Thank you! :clap: (I like this function)
Your solution works in most cases,
No problems - now (as far as I understand) arise if the input value has decimals.
but I add the possibility to define if the number has decimals or not. :)
like this .:

Code: Select all

currencyToDecimal(currency, decsign := ".", dec := 2, decExist := "y") {
 ; Version 05
 ; @mikeyww on 11 October 2020, revised 12 October 2020 @Albireo
 ; 2021/01/17: fixed: cents could not be identified in currency with more than two decimal places
 ; 2021/01/16: fixed: comma could not be used as a decimal sign
 ; 2021/01/03: added: decimal places
 ; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=81983
 
 if !(decExist = "y"), currency := currency ".0"
 currency := Trim(currency), RegExMatch(currency, ".*[.,]\K\d*$", cents)
 If (cents = "")
  currency .= ".", cents := "00"
 RegExMatch(currency, ".+(?=[.,])", dollars)
 Return StrReplace(Format("{:0." dec "f}", RegExReplace(dollars, "[ .,]") "." cents), ".", decsign)
}
User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Convert many currency format to decimals

17 Jan 2021, 11:46

I do not know how this helps, but great if it does!
Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

Re: Convert many currency format to decimals

18 Jan 2021, 08:27

Thank you!
Version 4 is better, (with the addition of the ability to specify whether the input value is an integer or not decExit)
The only time the input value must be specified because the input value is an integer in version 4 is for example. 123 456 (Which I think happens quite rarely)
Version 4 handle 1,000 1,000 1,000 without any problems.

Version 5 handles (1,000) One thousand, as One point zero zero zero -
User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Convert many currency format to decimals

18 Jan 2021, 09:25

Code: Select all

currency := ["1.000,12", "1,000.34", "1 000,45", "1000", "1,000", "200,45", "200,00", "10.0", "3.030303", "123 456"]
For index, this in currency
 ttext .= "`n" this " => " currencyToDecimal(this, ".", 3)
MsgBox, 64, Currency, % SubStr(ttext, 2)
ExitApp

currencyToDecimal(currency, decSign := ".", dec := 2) {
 ; Version 06
 ; @mikeyww on 11 October 2020, revised 12 October 2020 @Albireo
 ; 2021/01/18: fixed: integers with spaces were not properly recognized
 ; 2021/01/17: fixed: cents could not be identified in currency with more than two decimal places
 ; 2021/01/16: fixed: comma could not be used as a decimal sign
 ; 2021/01/03: added: decimal places
 ; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=81983
 d1 := Instr(currency, ","), d2 := Instr(currency, ".")
 decSign := d1 && d2 ? SubStr(currency, Max(d1, d2), 1) : decSign
 currency := Trim(currency), RegExMatch(currency, ".*\" decSign "\K\d*$", cents)
 If (cents = "")
  currency .= ".", cents := "00"
 RegExMatch(currency, ".+(?=[.,])", dollars)
 Return Format("{:0." dec "f}", RegExReplace(dollars, "[ .,]") "." cents)
}
Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

Re: Convert many currency format to decimals

18 Jun 2021, 05:50

Hi!
Would it be complicated to add a parameter in this function() - from decimal comma/point to decimal comma/point?
eg.

Code: Select all

currencyToDecimal(currency, frSign := ".", toSign := ",", dec := 2)	; From decimal point to decimal comma
{...
}
currencyToDecimal(currency, ",", ".",  2)	; eg. From decimal comma to decimal point
I can solve the problem this way below (but it would be easier / nicer / better if currencyToDecimal() could handle this

Code: Select all

InCur1 := "64.5"
OutCur1 := StrReplace(currencyToDecimal(InCur1, ".", 2), ".", ",")
; StringReplace OutCur1, currencyToDecimal(InCur1, ".", ",", 2) ., `,	; 64,50

InCur2 := "123"
OutCur2 := StrReplace(currencyToDecimal(InCur2, ".", 2), ".", ",")
; StringReplace OutCur2, currencyToDecimal(InCur2, ".", ",", 2), ., `,	; 123,00

InCur3 := "74.456"
OutCur3 := StrReplace(currencyToDecimal(InCur3, ".", 2), ".", ",")
; StringReplace OutCur3, currencyToDecimal(InCur2, ".", ",", 2), ., `,	; 74,46

MsgBox % OutCur1 "`n" OutCur2 "`n" OutCur3 ;%
ExitApp

currencyToDecimal(currency, decSign := ".", dec := 2) {
 ; Version 06
 ; @mikeyww on 11 October 2020, revised 12 October 2020 @Albireo
 ; 2021/01/18: fixed: integers with spaces were not properly recognized
 ; 2021/01/17: fixed: cents could not be identified in currency with more than two decimal places
 ; 2021/01/16: fixed: comma could not be used as a decimal sign
 ; 2021/01/03: added: decimal places
 ; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=81983
 d1 := Instr(currency, ","), d2 := Instr(currency, ".")
 decSign := d1 && d2 ? SubStr(currency, Max(d1, d2), 1) : decSign
 currency := Trim(currency), RegExMatch(currency, ".*\" decSign "\K\d*$", cents)
 If (cents = "")
  currency .= ".", cents := "00"
 RegExMatch(currency, ".+(?=[.,])", dollars)
 Return Format("{:0." dec "f}", RegExReplace(dollars, "[ .,]") "." cents)
}
User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Convert many currency format to decimals

18 Jun 2021, 07:04

The "from" is already there as the decsign, though the script adds a bit of auto-detection to this. The "to" should be straightforward to add, because if you want ",", you could just replace "." with "," in the final output. You could use one new function parameter to handle that.
Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

Re: Convert many currency format to decimals

18 Jun 2021, 08:13

I don't understand, what you mean!
I've tried this .:

Code: Select all

#SingleInstance force

InCur1 := "64.5"
OutCur1 := currToDec(InCur1, ".", ",", 2)
; InCur1 := "64,5"
; OutCur1 := currToDec(InCur1, ",", ".", 2)

InCur2 := "123"
OutCur2 := currToDec(InCur2, ".", ",", 2)

InCur3 := "74.456"
OutCur3 := currToDec(InCur3, ".", ",", 2)

MsgBox % OutCur1 "`n" OutCur2 "`n" OutCur3 ;%
ExitApp

currToDec(currency, frDec := ".", toDec := ".", dec := 2) {
 d1 := Instr(currency, ","), d2 := Instr(currency, ".")
 frDec := d1 && d2 ? SubStr(currency, Max(d1, d2), 1) : frDec
 currency := Trim(currency), RegExMatch(currency, ".*\" frDec "\K\d*$", cents)
 If (cents = "")
  currency .= ".", cents := "00"
 RegExMatch(currency, ".+(?=[.,])", dollars)
 If (toDec = ".")
	Return Format("{:0." dec "f}", RegExReplace(dollars, "[ .,]") "." cents)
 else
	Return Format("{:0," dec "f}", RegExReplace(dollars, "[ .,]") "," cents)
}
Only toDec = "." works (as before)

And that .:

Code: Select all

Return Format("{:0" toDec dec "f}", RegExReplace(dollars, "[ .,]") toDec cents)
User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Convert many currency format to decimals  Topic is solved

18 Jun 2021, 08:46

Code: Select all

For index, this in ["64.5", "123", "74.456"]
 ttext .= "`n" this " => " currencyToDecimal(this, ".", ",")
MsgBox, 64, Currency, % SubStr(ttext, 2)
ExitApp

currencyToDecimal(currency, decSign := ".", toDec := ".", dec := 2) {
 /* Version 07 ------------------------------------------
 Convert currency formats based on use of decimal points and commas
 @mikeyww on 11 October 2020, revised 12 October 2020 @Albireo
 currency = Currency string to convert (i.e., input string)
 decSign  = Character indicating the decimal place in the input string
 toDec    = Character indicating the decimal place in the output string
 dec      = Number of decimal places in the output string
 2021/06/18: added: toDec parameter specifies the output format
 2021/01/18: fixed: integers with spaces were not properly recognized
 2021/01/17: fixed: cents could not be identified in currency with more than two decimal places
 2021/01/16: fixed: comma could not be used as a decimal sign
 2021/01/03: added: decimal places
 https://www.autohotkey.com/boards/viewtopic.php?f=76&t=81983
 --------------------------------------------------------
 */
 d1 := Instr(currency, ","), d2 := Instr(currency, ".")
 ; If both comma and dot are present, then decSign is the last one:
   decSign := d1 && d2 ? SubStr(currency, Max(d1, d2), 1) : decSign
 ; Get the cents after the decSign:
   currency := Trim(currency), RegExMatch(currency, ".*\" decSign "\K\d*$", cents)
 If (cents = "")
  currency .= ".", cents := "00"
 RegExMatch(currency, ".+(?=[.,])", dollars) ; Get the dollars (all up to the last comma or dot)
 Return StrReplace(Format("{:0." dec "f}", RegExReplace(dollars, "[ .,]") "." cents), ".", toDec)
}
Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

Re: Convert many currency format to decimals

19 Jun 2021, 00:30

OK!
That's how you thought :D

Seems to be working!

Thank you! :dance:
User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Convert many currency format to decimals

19 Jun 2021, 06:08

I had your original solution in mind. When I tried it, I realized that the original "." was needed to make the Format command work properly. The fix was to leave Format intact and then change its output.

I'm glad it works!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Auntiejack56, Descolada, ntepa, Xtra and 140 guests