Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

6.04 Converting Data Types

Converting Data Types

Crystal syntax is a type safe language that requires all constants and variables in the same formula to be of the same data type. It also requires you to pass constants and variables as arguments using the exact data type that the formula expects. Even though the data types in Crystal syntax are fairly simple, you still have to make sure that they are compatible. Fortunately, this shouldn’t cause problems because there are functions to convert between the different data types. Table 6-5 lists these conversion functions.

Table 6-5. Conversion Functions

Conversion Function Description
CBool(number), CBool(currency) Convert to Boolean.
CCur(number), CCur(string) Convert to Currency.
CDbl(currency), CDbl(string),CDbl(boolean) Convert to Number. Equivalent to ToNumber(). See the section “Formatting Values for Output”.
CStr() Convert to String. Equivalent to ToText().
CDate(string), CDate(year, month, day), CDate(DateTime) Convert to Date.
CTime(string), CTime(hour, min, sec), CDate(DateTime) Convert to Time.
CDateTime(string),CDateTime(date),CDateTime(date, time),CDateTime(year, month, day) Convert to DateTime.
CDateTime(year, month, day, hour,min, sec) Convert to DateTime.
ToNumber(string), ToNumber(Boolean) Convert to a Number.
ToText() Convert to String. Same as CStr().
IsDate(string), IsTIme(), IsDateTime() Test a string for being a valid date/time.
ToWords(number),ToWords(number, decimals) Convert a number to its word equivalent.

Most of the above functions are very simple. Pass the function a field/variable of one data type and it returns the equivalent in the other data type. The CBool() function takes a number or currency value and converts it to Boolean True or False. Any non-zero value is converted to True and zero is converted to False. When it is displayed on a report, it prints the words “True” or “False”.

The CCur() function takes a number or string and converts it to the Currency data type. When converting a string, it can have formatting characters in it (“$”, “,”, etc.) and it will still be converted properly.

The CDbl() and ToNumber() functions are equivalent. Pass each a value and it gets converted to a number.

The CDate(), CTime() and CDateTime() are all similar. Pass them a string and it gets converted to the proper data type. The string parser for this function is very sophisticated. It lets you pass strings as diverse as “Jan 19, 1991”, “5/26/1998” and “2002, Feb 04”. You can also pass numbers as individual arguments for representing the different parts of a date and time. See Table 6-5 for the various argument options.

When converting a string to a date or number, you run the risk of raising an error if the string isn’t in the expected format. You can avoid this by testing the strings validity before converting it. The IsDate() and IsNumber() functions do this for you. They return True if the string can be properly converted. If not, they return False. For example, here is a function that converts a string to a date, but only if it is a valid date.

If IsDate({Invoice.ExpirationDate}) Then
CDate({Invoice.ExpirationDate});

The ToWords() function takes a number and converts it to its equivalent in words. This is similar to writing a dollar amount on a check and then spelling out the full amount in words. It prints the decimal portion as “##/100”. You can set the number of decimals it displays by passing a number to the second argument, which is optional. Notice in the second example how it only displays one decimal place and it rounds it up to the next higher number.

//Demonstrate the ToWords() formula
ToWords(123.45); //Result is “one hundred twenty-three 45 / 100”
ToWords(123.45,1); //Result is “one hundred twenty-three and 5 / 100”