KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: The little used abilities of the 4D Num function
PRODUCT: 4D | VERSION: 13.0 | PLATFORM: Mac & Win
Published On: August 13, 2012

The 4D Num function has to ability to add great flexibility to a developers toolbox. However with the flexibility the Num function provides, there is a gotcha the developer needs to be aware of in its use.

Little standard use of the Num function


The Num function falls under the String theme of 4D commands. Its most common use it to turn a stirng of numerical characters in to a numerical value, Integer, Longint, Real or Float.

C_LONGINT(MyLong_L)
C_REAL(MyReal_R)

MyLong_L:=Num("123456")
MyReal_R:=Num("123.456")


Little used flexibility of the Num function


The Num function also has the ability to "parse" a number out of a string of mixed alpha and numeric characters. Such as:

MyLong_L:=Num("A1B2C3") // MyLong_L gets 123
MyReal_R:=Num("a1.b2c3") // MyReal_R gets 1.23


This flexibilty gives a developer great flexibility in a number of ways...

  • A string can be encoded with embeded numbers, such as an automobile VIN number, that when tested using Num can determin is the stirng is valid or invalid,

  • A string can be encoded with embeded numbers to where when the number is in a certain ranged it indecates a different class of product.

  • A form with a lot of buttons can have the buttons named with an encluded number, such as "Btn_001", "Btn_010", "Btn_100" and then the resulting number can determine the action to take. See the example below...


$Btn_Index_L:=Num(Object name(Current object))

Case of
   :($Btn_Index_L=1)
    // Do something
   :($Btn_Index_L=10)
    // Do something
   :($Btn_Index_L=100)
    // Do something
   Else
    // Do something
End case


The "gotcha"


The gotcha is how the form object name is encoded. Using a "dot", as in "Btn.001", "Btn.010", "Btn.100", etc. can be problematic. If the expected return value is an integer then Num is always going to return zero and the Case statement will always fall to Else.

Also the developer needs to be aware of the use of the "e" or "E" character. For example Num("A1B2C3") is going to return 123 but Num("A1B2E3") is going to return 12000.

Paying attention to these detail will produce very great results in the use of the Num function.