Box (1,1) (5,70) "Currency Formatter"
// InputFields for input
InputField (2,2) "Amount" (2,17) name="amount" size=20
InputField (3,2) "Currency" (3,17) name="currency" size=20
// Button to compare the texts
PushButton (4,2) "@TZ@Format" size=(1,10) process="format_currency.txt"
Using pAmt = "&V[amount]"
Using pCur = "&V[currency]"
This example shows an InputScript using using. While the InputScript is built to be also called via Process, in this example it is called via using in a PushButton.
format_currency.txt
GuiXT
Parameter pAmt
Parameter pCur
// Transform 'using' variables
if U[pAmt]
Set V[pAmt] "&U[pAmt]"
endif
if U[pCur]
Set V[pCur] "&U[pCur]"
endif
// Check for empty currency value
if V[pCur=]
Message "E: No currency given!" -statusline
Return
endif
// Check for empty amount value
if V[pAmt=]
Set V[pAmt] 0
endif
// Set currency to upperCase for consistent processing
Set V[pCur] "&V[pCur]" -upperCase
// Process decimal in amount
Set V[int] "&V[pAmt]"
Set V[dec] 0
Set V[sepCom] "&V[pAmt]" search="," -charPosition
Set V[sepDot] "&V[pAmt]" search="." -charPosition
if V[sepCom=0] and V[sepDot=0]
Set V[dec] "00"
goto post-separation
endif
if V[sepCom>&V[sepDot]]
Set V[sepChr] ","
else
Set V[sepChr] "."
endif
Set V[int] "&V[pAmt]" regex="\&V[sepChr]\d+" regexReplace=""
Set V[dec] "&V[pAmt]" regex="[\d,\.]+\&V[sepChr]" regexReplace=""
// Round decimal part
Set V[decLen] "&V[dec]" -charCount
if V[decLen=0]
Set V[dec] "00"
endif
if V[decLen=1]
Set V[dec] "&V[dec]0"
endif
if V[decLen>2]
Set V[dec] "&V[dec]" regex="^\d{3}"
Set V[dec] "&V[dec]" / 1000 decimals=2
Set V[dec] "&V[dec]" regex="0\.|0," regexReplace=""
endif
label post-separation
// Remove integer separators
Set V[int] "&V[int]" regex=",|\." regexReplace=""
// Reassemble with currency-appropriate decimal separator
if V[pCur=EUR]
Set V[rAmt] "&V[int],&V[dec]"
else
Set V[rAmt] "&V[int].&V[dec]"
endif
if V[pCur=USD] or V[pCur=GBP] or V[pCur=AUD]
Set V[rAmt] "$&V[rAmt]"
Return "&V[rAmt]"
endif
if V[pCur=EUR]
Set V[rAmt] "&V[rAmt]€"
Return "&V[rAmt]"
endif
Message "E: Invalid currency value &V[pCur]" -statusline