QUANTIZE

The QUANTIZE function returns a DECFLOAT value that is equal in value (except for any rounding) and sign to the first argument and that has an exponent that is set to equal the exponent of the second argument.

Read syntax diagram
>>-QUANTIZE(expression-1,expression-2)-------------------------><

The schema is SYSIBM.

The number of digits that is returned (16 or 34) is the same as the number of digits in expression-1.
expression-1
The argument must be an expression that returns a value of any built-in numeric data type. If the argument is not a DECFLOAT value, it is converted to DECFLOAT(34) for processing.

Start of changeThe argument can also be a character string or graphic string data type. The string input is implicitly cast to a numeric value of DECFLOAT(34).End of change

expression-2
The argument must be an expression that returns a value of any built-in numeric data type. If the argument is not a DECFLOAT value, it is converted to DECFLOAT(34) for processing. expression-2 is an expression that is used as an example pattern that will be used to rescale expression-1. The sign and coefficient of the second argument are ignored.

Start of changeThe argument can also be a character string or graphic string data type. The string input is implicitly cast to a numeric value of DECFLOAT(34).End of change

If one argument (after conversion) is DECFLOAT(16) and the other is DECFLOAT(34), the DECFLOAT(16) argument is converted to DECFLOAT(34) before the function is processed.

The coefficient of the result is derived from that of expression-1. It is rounded, if necessary (if the exponent is being increased), multiplied by a power of ten (if the exponent is being decreased), or remains unchanged (if the exponent is already equal to that of expression-2).

For static SQL statements other than CREATE VIEW, the ROUNDING bind option or the native SQL procedure option determines the rounding mode.

For dynamic SQL statements (and static CREATE VIEW statements), the special register CURRENT DECFLOAT ROUNDING MODE determines the rounding mode.

Unlike other arithmetic operations on the DECFLOAT data type, if the length of the coefficient after the quantize operation is greater than the precision specified by expression-2, a warning occurs. This ensures that, unless there is an error condition, the exponent of the result of QUANTIZE is always equal to that of expression-2. Furthermore:

  • If either argument is NaN, NaN is returned
  • If either argument is sNaN, NaN is returned and an exception occurs
  • If both arguments are infinity (positive or negative), infinity (positive or negative) is returned.
  • If one argument is infinity (positive or negative) and the other argument is not infinity (positive or negative), NaN is returned and an exception occurs

The result of the function is a DECFLOAT(16) value if both arguments are DECFLOAT(16). Otherwise, the result of the function is a DECFLOAT(34) value.

The result can be null; if any argument is null, the result is the null value.

Examples: The following examples illustrate the value that is returned for the QUANTIZE function given the input DECFLOAT values:
   QUANTIZE(2.17, DECFLOAT(0.001))   = 2.170
   QUANTIZE(2.17, DECFLOAT(0.01))    = 2.17
   QUANTIZE(2.17, DECFLOAT(0.1))     = 2.2
   QUANTIZE(2.17, DECFLOAT('1E+0'))  = 2
   QUANTIZE(2.17, DECFLOAT('1E+1'))  = 0E+1
   QUANTIZE(2, DECFLOAT(INFINITY))   = NAN   –- exception
   QUANTIZE(-0.1, DECFLOAT(1) )      = 0
   QUANTIZE(0, DECFLOAT('1E+5'))     = 0E+5
   QUANTIZE(217, DECFLOAT('1E-1'))   = 217.0
   QUANTIZE(217, DECFLOAT('1E+0'))   = 217
   QUANTIZE(217, DECFLOAT('1E+1'))   = 2.2E+2
   QUANTIZE(217, DECFLOAT('1E+2'))   = 2E+2