Simple Data Types
Crystal syntax supports the standard data types that we expect in a language: number, Boolean, currency, string, date and time.
Table 7-1. Data Type Default Values
Crystal Data Type | Basic Data Type | Default Value |
---|---|---|
NumberVar | Number | 0 |
CurrencyVar | Currency | $0 |
BooleanVar | Boolean | False |
StringVar | String | “” |
DateVar | Date | Date(0,0,0) – The Null Date value 00/00/00 |
TimeVar | Time | No default value. Null. |
DateTimeVar | DateTime | No default value. Null. |
Notice that rather than have a large number of numeric data types such as integer, double, etc., there is simply a single data type called NumberVar. There is no need to worry about whether the number is fractional or what its largest possible value is. One data type handles all situations.
The CurrencyVar data type is treated the same as a NumberVar data type with a few exceptions:
Currency can only have two decimal places. If assigned a number with more than two decimal places, it rounds up to the nearest penny.
Currency automatically gets formatted as a monetary value. This eliminates the overhead of having to format the variable whenever it gets printed.
Since Currency is a different data type, it must be converted to a number to be used in mathematical assignments using non-currency variables. See the section “Converting Data Types” for more information.
Strings use the double quote, “, to specify a string literal. A character is represented by a string of length one. Referencing a position within a string is Base 1. Thus, if you want to refer to the first character in a string, you would use an index of 1. The maximum length of a string constant is 65,534 characters. Information on using the built-in string functions is in Chapter 6.
Dates are a little unusual in that there are three different data types available. The DateVar type can only store a date and the TimeVar type can only store a time. It’s preferable to use these data types if you don’t need both values stored in a variable. If you do need both types in the same variable, use the DateTimeVar type. To assign a constant to a DateTimeVar , use the Date() function. You can use a variety of formats to pass the date to the function.