Updated on 2024-08-20 GMT+08:00

Numeric Types

Table 1 lists the common APIs for numeric (numeric or decimal) data provided by the ECPG.

Table 1 Common numeric type APIs

API

Description

Remarks

numeric* PGTYPESnumeric_new(void)

Requests a pointer to a newly allocated numeric variable.

This function creates a numeric variable on the heap. The return value is of the numeric* type.

decimal* PGTYPESdecimal_new(void)

Requests a pointer to a newly allocated decimal variable.

This function creates a decimal variable on the heap. The return value is of the decimal* type.

void PGTYPESnumeric_free(numeric* var)

Frees the memory of a variable of the numeric type.

This function frees a numeric* variable created by using the PGTYPESnumeric_new function.

void PGTYPESdecimal_free(decimal*)

Frees the memory of a variable of the decimal type.

This function frees a decimal* variable created by using the PGTYPESdecimal_new function.

numeric* PGTYPESnumeric_from_asc(char* str, char** endptr)

Converts a string to the numeric type.

For example, the valid formats are –2, .794, +3.44, 592.49E07, or –32.84e–4. If the value could be parsed successfully, a valid pointer is returned; otherwise, a null pointer is returned. ECPG only parses complete character strings. Currently, ECPG does not support storing the address of the first invalid character in *endptr. However, endptr can be set to null.

char* PGTYPESnumeric_to_asc(numeric* num, int dscale)

Returns a pointer to a string allocated by malloc that contains the string representation of the numeric type num.

The numeric value will be printed using dscale decimal places and will be rounded if necessary.

int PGTYPESnumeric_add(numeric* var1, numeric* var2, numeric* result)

Adds two numeric variables to the third numeric variable.

This function adds the variables var1 and var2 to the result variable result. The function returns 0 on success and –1 in case of error.

int PGTYPESnumeric_sub(numeric* var1, numeric* var2, numeric* result)

Subtracts two numeric variables and returns the result to the third numeric variable.

This function subtracts var2 from var1. The result of this operation is stored in the variable result. The function returns 0 on success and –1 in case of error.

int PGTYPESnumeric_mul(numeric* var1, numeric* var2, numeric* result)

Multiplies two numeric variables and returns the result to the third numeric variable.

This function multiplies variables var1 and var2. The result of this operation is stored in the variable result. The function returns 0 on success and –1 in case of error.

int PGTYPESnumeric_div(numeric* var1, numeric* var2, numeric* result)

Divides two numeric variables and returns the result to the third numeric variable.

This function divides variable var1 by variable var2. The result of this operation is stored in the variable result. The function returns 0 on success and –1 in case of error.

int PGTYPESnumeric_cmp(numeric* var1, numeric* var2)

Compares two numeric variables.

This function compares two numeric variables. If an error occurs, INT_MAX is returned. On success, this function returns one of the following three possible results:
  • If var1 is greater than var2, 1 is returned.
  • If var1 is smaller than var2, –1 is returned.
  • If var1 and var2 are equal, 0 is returned.

int PGTYPESnumeric_from_int(signed int int_val, numeric* var)

Converts an int variable into a numeric variable.

This function accepts a signed int variable and stores it in the numeric variable var. The function returns 0 on success and –1 in case of failure.

int PGTYPESnumeric_from_long(signed long int long_val, numeric* var)

Converts a long int variable into a numeric variable.

This function accepts a long int variable and stores it in the numeric variable var. The function returns 0 on success and –1 in case of failure.

int PGTYPESnumeric_copy(numeric* src, numeric* dst)

Copies a numeric variable to another one.

This function copies the value of the variable that src points to into the variable that dst points to. The function returns 0 on success and –1 in case of error.

int PGTYPESnumeric_from_double(double d, numeric* dst)

Converts a double-precision variable into a numeric variable.

This function accepts a double-precision variable and stores the result in the variable that dst points to. The function returns 0 on success and –1 in case of error.

int PGTYPESnumeric_to_double(numeric* nv, double* dp)

Converts a numeric variable into a double-precision variable.

This function converts the numeric value in the variable that nv points to into a double-precision variable that dp points to. The function returns 0 on success and –1 in case of error (or overflow). When overflow occurs, the global variable errno is additionally set to PGTYPES_NUM_OVERFLOW.

int PGTYPESnumeric_to_int(numeric* nv, int* ip)

Converts a numeric variable into an int variable.

This function converts the numeric value in the variable that nv points to into an int variable that the IP address points to. The function returns 0 on success and –1 in case of error (or overflow). When overflow occurs, the global variable errno is additionally set to PGTYPES_NUM_OVERFLOW.

int PGTYPESnumeric_to_long(numeric* nv, long* ip)

Converts a numeric variable into a long int variable.

This function converts the numeric value in the variable that nv points to into a long int variable that the IP points to. The function returns 0 on success and –1 in case of error (or overflow). When overflow occurs, the global variable errno is additionally set to PGTYPES_NUM_OVERFLOW.

int PGTYPESnumeric_to_decimal(numeric* src, decimal* dst)

Converts a numeric variable into a decimal variable.

This function converts the numeric value in the variable that src points to into a decimal variable that dst points to. The function returns 0 on success and –1 in case of error (or overflow). When overflow occurs, the global variable errno is additionally set to PGTYPES_NUM_OVERFLOW.

int PGTYPESnumeric_from_decimal(decimal* src, numeric* dst)

Converts a decimal variable into a numeric variable.

This function converts the decimal value in the variable that src points to into a numeric variable that dst points to. The function returns 0 on success and –1 in case of error (or overflow).

Examples

For details, see Examples.