KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Entering totals without having to enter the decimal place
PRODUCT: 4D | VERSION: 13.2 | PLATFORM: Mac & Win
Published On: February 15, 2013

Some systems make it easier for users to enter totals, making it so they do not need to worry about adding in the extra commas or the decimal place. For example, if the user wants to enter a total for 1,999.99 dollars, the user can simply type the numbers only and let the code in the background take care of adding in the decimal and commas when needed.

This can be done using a text variable and the form event "On After Keystroke." Below is example code that will filter keystrokes and format the numeric string as a user enters in a total. For this example the name of the text variable is "total_t."

Case of
   : (Form event=On Load)
     C_TEXT(total_t)
     total_t:="0.00"
   : (Form event=On After Keystroke)
     //Get the character just entered by the user
     $input:=Keystroke
     //If the character entered is not a number or backspace,
     //filter the key such that nothing happens
     If ($input#"0") & (Num($input)=0) & (Character code(Keystroke)#Backspace)
        FILTER KEYSTROKE("")
     End if
     //Code to format the numeric string
     total_t:=Replace string(Get edited text;".";"")
     total_t:=Replace string(total_t;",";"")
     total_t:=Insert string(total_t;".";Length(total_t)-1)
     total_t:=String(Num(total_t);"#,###,###,##0.00")
     //Keep the cursor at the end
     $endPos:=Length(total_t)+1
     HIGHLIGHT TEXT(total_t;$endPos;$endPos)
End case


Below shows how the formatting changes as a user enters a total.
Initial value displayed: 0.00
Value displayed after user enters "1": 0.01
Value displayed after user enters "2": 0.12
Value displayed after user enters "3": 1.23
Value displayed after user enters "4": 12.34
Value displayed after user enters "5": 123.45
Value displayed after user enters "6": 1,234.56
Value displayed after user enters "a": 1,234.56
//The code filters out all keystrokes that are not numeric or backspace