Type conversions in AdvPL
Chooses explicit formats for date and number conversion.
Overview
CTOD interprets date text under environment formatting and DTOC displays it; ambiguous text is not portable. DTOS produces YYYYMMDD and STOD reverses that contract. STR pads spaces and StrZero pads zeros; insufficient width can produce asterisks. Val reads a numeric prefix rather than validating the whole input: decimal points and trailing text need handling. This example uses an unambiguous date and fixed number. (Sources: refs. 1–7.)
Commented example
#Include "Protheus.ch"
User Function Ed408()
Local dDate := SToD("20261026")
// Explicit interchange format without changing SET DATE.
ConOut(DToS(dDate))
ConOut(DToC(dDate))
ConOut(cValToChar(CToD(DToC(dDate)) == dDate))
ConOut(Str(12,4,0))
ConOut(StrZero(12,4,0))
ConOut(cValToChar(Val("12.5")))
Return Nil20261026; environment-formatted date; true equality under prepared formatting; " 12"; "0012"; number 12.5. Under review: editorial example neither compiled nor run in Protheus. Check includes, release, RDD and context before testing.
- Validate input before conversion and check width, separator and century.
- Under review: editorial example neither compiled nor run in Protheus. Check includes, release, RDD and context before testing.

Comments
There are no approved comments yet.
Sign in with Google or Microsoft to comment.