Caché ObjectScript Reference
[Back] [Next]
Go to:

Returns a number converted to a Caché floating point value.
num The numeric value to be converted. Commonly this is an IEEE floating point number.
digits Optional — An integer that specifies the number of significant digits to return. $DECIMAL rounds the return value to that number of digits, using the IEEE floating point rounding algorithm. Valid values are 1 through 38, and 0. If digits is greater than the number of digits the value is returned unchanged. If digits is 0, no rounding is performed on num unless it has more than 20 significant digits (see below for details on 0 value).
$DECIMAL returns a floating-point number converted to the Caché decimal floating-point data type. This function is used to convert a fractional number in IEEE double-precision (64-bit) binary floating-point format to the corresponding fractional number in Caché decimal floating-point format. This is the inverse of the operation performed by the $DOUBLE function.
The Caché SQL data types DOUBLE and DOUBLE PRECISION represent IEEE floating point numbers; the FLOAT data type represents standard Caché fractional numbers.
IEEE floating point numbers are represented internally using 53 binary bits. Because most fractional decimal numbers have no exact binary representation, a fractional number in $DOUBLE format will usually differ slightly from its $DECIMAL conversion. Standard Caché fractional numbers have an approximate precision of 18.96 decimal digits on all supported Caché system platforms. When an IEEE floating point number is displayed as a fractional number the binary bits are often converted to a fractional number with far more than 18 decimal digits. This does not mean that IEEE floating point numbers are more precise than standard Caché fractional numbers.
The num value can be specified as a number or a numeric string. It is resolved to canonical form (leading and trailing zeros removed, multiple plus and minus signs resolved, etc.) before $DECIMAL conversion. If the num value is outside of the range of values that can be converted to FLOAT data type, $DECIMAL generates a <MAXNUMBER> error. Specifying a nonnumeric string to num returns 0. Specifying a mixed-numeric string (for example "7dwarves" or "7.5.4") to num truncates the input value at the first nonnumeric character then converts the numeric portion.
Default rounding is done as follows:
The digits argument can be used to round the return value to a specified number of digits. Trailing zeros are always deleted. If digits is a positive integer, rounding is done using the IEEE rounding standard. If num has more than 38 significant digits (and digits=38) $DECIMAL rounds the fractional portion of the number at the 38th digit and represents all of the following num digits with zeros. If digits is greater than 38, an <ILLEGAL VALUE> error is generated.
If digits is 0, the number is cast to string collation; this is equivalent to (+num)_"". If num is 20 digits or less, digits=0 is the same as digits=20.
However, if digits is 0 and num is more than 20 digits, special rounding (not IEEE rounding) is performed, as follows. Rounding is performed to return 20 digits. Special rounding is then performed on the 20th digit if it rounds to a 0 or a 5. In the case when the 20th digit would round up to a 0 or a 5, Caché rounds it down to a 9 or a 4, respectively. In the case when the 20th digit would round down to a 0 or a 5, Caché rounds it up to a 1 or a 6, respectively. Other 20th digit values are returned unchanged. This rounding algorithm is used to provide correct numeric collation and avoid rounding inconsistencies.
Integer Divide
With certain values, Caché decimal floating-point and IEEE double numbers yield a different integer divide product. For example:
  WRITE !,"Integer divide operations:"
  WRITE !,"Cache  \: ",$DECIMAL(4.1)\.01  // 410
  WRITE !,"Double \: ",$DOUBLE(4.1)\.01   // 409
For further details on arithmetic operations involving IEEE double numbers, see the appendix Numeric Computing in InterSystems Applications in the Caché Programming Orientation Guide.
If num is INF, a <MAXNUMBER> error is generated. If num is NAN, an <ILLEGAL VALUE> error is generated. These invalid values are shown in the following example:
The following example demonstrates that $DECIMAL has no effect when applied to a fractional number that is already in Caché format:
  SET y=$ZPI
    IF x=y { WRITE !,"Identical:"
             WRITE !,"Cache $DECIMAL: ",x
             WRITE !,"Native Cache:   ",y }
   ELSE { WRITE !,"Different:"
          WRITE !,"Cache $DECIMAL: ",x
          WRITE !,"Native Cache:   ",y }
The following example returns the value of pi as a $DOUBLE value and as a standard Caché numeric value. This example shows that equality operations should not be attempted between $DOUBLE and standard Caché numbers, and that equivalence cannot be restored by using $DECIMAL to convert IEEE back to Caché:
  IF x=y { WRITE !,"Cache & IEEE Same" }
  ELSEIF x=z { WRITE !,"Cache & IEEE-to-Cache same" }
  ELSE { WRITE !,"All three different"
         WRITE !,"Cache decimal: ",x
         WRITE !,"IEEE float:    ",y
         WRITE !,"IEEE to Cache: ",z }
The following example returns the $DECIMAL conversion of pi as a $DOUBLE value. These conversions are rounded by different digits argument values:
   /* returns 3.141592653589793116 (19 digits) */
   /* returns 3 */
   /* returns 3.1415927 (note rounding) */
  WRITE !,$DECIMAL(x,12)
   /* returns 3.14159265359 (note rounding) */
  WRITE !,$DECIMAL(x,18)
   /* returns 3.14159265358979312 */
  WRITE !,$DECIMAL(x,19)
   /* returns 3.141592653589793116 (19 digits) */
  WRITE !,$DECIMAL(x,20)
   /* returns 3.141592653589793116 (19 digits) */
  WRITE !,$DECIMAL(x,21)
   /* returns 3.141592653589793116 (19 digits) */
   /* returns 3.1415926535897931159 (20 digits) */
See Also