| PostgreSQL Reference Manual - Volume 2 - Programming Guide by The PostgreSQL Global Development Group Paperback (6"x9"), 408 pages ISBN 0954612035 RRP £19.95 ($34.95) Sales of this book support the PostgreSQL project! Get a printed copy>>> |
3.8.1 The numeric type
The numeric type offers to do calculations with arbitrary precision. See
Volume 1: Numeric Types for the equivalent type in the
PostgreSQL server. Because of the arbitrary precision this
variable needs to be able to expand and shrink dynamically. That's why you
can only create variables on the heap by means of the
PGTYPESnumeric_new and PGTYPESnumeric_free
functions. The decimal type, which is similar but limited in the precision,
can be created on the stack as well as on the heap.
The following functions can be used to work with the numeric type:
PGTYPESnumeric_new-
Request a pointer to a newly allocated numeric variable.
numeric *PGTYPESnumeric_new(void);
PGTYPESnumeric_free-
Free a numeric type, release all of its memory.
void PGTYPESnumeric_free(numeric *var);
PGTYPESnumeric_from_asc-
Parse a numeric type from its string notation.
numeric *PGTYPESnumeric_from_asc(char *str, char **endptr);
Valid formats are for example:-2,.794,+3.44,592.49E07or-32.84e-4. If the value could be parsed successfully, a valid pointer is returned, else the NULL pointer. At the moment ecpg always parses the complete string and so it currently does not support to store the address of the first invalid character in*endptr. You can safely setendptrto NULL. PGTYPESnumeric_to_asc-
Returns a pointer to a string allocated by
mallocthat contains the string representation of the numeric typenum.char *PGTYPESnumeric_to_asc(numeric *num, int dscale);
The numeric value will be printed withdscaledecimal digits, with rounding applied if necessary. PGTYPESnumeric_add-
Add two numeric variables into a third one.
int PGTYPESnumeric_add(numeric *var1, numeric *var2, numeric *result);
The function adds the variablesvar1andvar2into the result variableresult. The function returns 0 on success and -1 in case of error. PGTYPESnumeric_sub-
Subtract two numeric variables and return the result in a third one.
int PGTYPESnumeric_sub(numeric *var1, numeric *var2, numeric *result);
The function subtracts the variablevar2from the variablevar1. The result of the operation is stored in the variableresult. The function returns 0 on success and -1 in case of error. PGTYPESnumeric_mul-
Multiply two numeric variables and return the result in a third one.
int PGTYPESnumeric_mul(numeric *var1, numeric *var2, numeric *result);
The function multiplies the variablesvar1andvar2. The result of the operation is stored in the variableresult. The function returns 0 on success and -1 in case of error. PGTYPESnumeric_div-
Divide two numeric variables and return the result in a third one.
int PGTYPESnumeric_div(numeric *var1, numeric *var2, numeric *result);
The function divides the variablesvar1byvar2. The result of the operation is stored in the variableresult. The function returns 0 on success and -1 in case of error. PGTYPESnumeric_cmp-
Compare two numeric variables.
int PGTYPESnumeric_cmp(numeric *var1, numeric *var2)
This function compares two numeric variables. In case of error,INT_MAXis returned. On success, the function returns one of three possible results:-
1, if
var1is bigger thanvar2 -
-1, if
var1is smaller thanvar2 -
0, if
var1andvar2are equal
-
1, if
PGTYPESnumeric_from_int-
Convert an int variable to a numeric variable.
int PGTYPESnumeric_from_int(signed int int_val, numeric *var);
This function accepts a variable of type signed int and stores it in the numeric variablevar. Upon success, 0 is returned and -1 in case of a failure. PGTYPESnumeric_from_long-
Convert a long int variable to a numeric variable.
int PGTYPESnumeric_from_long(signed long int long_val, numeric *var);
This function accepts a variable of type signed long int and stores it in the numeric variablevar. Upon success, 0 is returned and -1 in case of a failure. PGTYPESnumeric_copy-
Copy over one numeric variable into another one.
int PGTYPESnumeric_copy(numeric *src, numeric *dst);
This function copies over the value of the variable thatsrcpoints to into the variable thatdstpoints to. It returns 0 on success and -1 if an error occurs. PGTYPESnumeric_from_double-
Convert a variable of type double to a numeric.
int PGTYPESnumeric_from_double(double d, numeric *dst);
This function accepts a variable of type double and stores the result in the variable thatdstpoints to. It returns 0 on success and -1 if an error occurs. PGTYPESnumeric_to_double-
Convert a variable of type numeric to double.
int PGTYPESnumeric_to_double(numeric *nv, double *dp)
The function converts the numeric value from the variable thatnvpoints to into the double variable thatdppoints to. It returns 0 on success and -1 if an error occurs, including overflow. On overflow, the global variableerrnowill be set toPGTYPES_NUM_OVERFLOWadditionally. PGTYPESnumeric_to_int-
Convert a variable of type numeric to int.
int PGTYPESnumeric_to_int(numeric *nv, int *ip);
The function converts the numeric value from the variable thatnvpoints to into the integer variable thatippoints to. It returns 0 on success and -1 if an error occurs, including overflow. On overflow, the global variableerrnowill be set toPGTYPES_NUM_OVERFLOWadditionally. PGTYPESnumeric_to_long-
Convert a variable of type numeric to long.
int PGTYPESnumeric_to_long(numeric *nv, long *lp);
The function converts the numeric value from the variable thatnvpoints to into the long integer variable thatlppoints to. It returns 0 on success and -1 if an error occurs, including overflow. On overflow, the global variableerrnowill be set toPGTYPES_NUM_OVERFLOWadditionally. PGTYPESnumeric_to_decimal-
Convert a variable of type numeric to decimal.
int PGTYPESnumeric_to_decimal(numeric *src, decimal *dst);
The function converts the numeric value from the variable thatsrcpoints to into the decimal variable thatdstpoints to. It returns 0 on success and -1 if an error occurs, including overflow. On overflow, the global variableerrnowill be set toPGTYPES_NUM_OVERFLOWadditionally. PGTYPESnumeric_from_decimal-
Convert a variable of type decimal to numeric.
int PGTYPESnumeric_from_decimal(decimal *src, numeric *dst);
The function converts the decimal value from the variable thatsrcpoints to into the numeric variable thatdstpoints to. It returns 0 on success and -1 if an error occurs. Since the decimal type is implemented as a limited version of the numeric type, overflow can not occur with this conversion.
| ISBN 0954612035 | PostgreSQL Reference Manual - Volume 2 - Programming Guide | See the print edition |