44 lines
1.1 KiB
Text
44 lines
1.1 KiB
Text
// Taken from https://www.postgresql.org/docs/13/ecpg-variables.html
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <pgtypes_numeric.h>
|
|
|
|
EXEC SQL WHENEVER SQLERROR STOP;
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
EXEC SQL BEGIN DECLARE SECTION;
|
|
numeric *num;
|
|
numeric *num2;
|
|
decimal *dec;
|
|
EXEC SQL END DECLARE SECTION;
|
|
|
|
EXEC SQL CONNECT TO testdb USER postgres;
|
|
|
|
num = PGTYPESnumeric_new();
|
|
dec = PGTYPESdecimal_new();
|
|
|
|
EXEC SQL SELECT 12.345::numeric(4,2), 23.456::decimal(4,2) INTO :num, :dec;
|
|
|
|
printf("numeric = %s\n", PGTYPESnumeric_to_asc(num, 0));
|
|
printf("numeric = %s\n", PGTYPESnumeric_to_asc(num, 1));
|
|
printf("numeric = %s\n", PGTYPESnumeric_to_asc(num, 2));
|
|
|
|
/* Convert decimal to numeric to show a decimal value. */
|
|
num2 = PGTYPESnumeric_new();
|
|
PGTYPESnumeric_from_decimal(dec, num2);
|
|
|
|
printf("decimal = %s\n", PGTYPESnumeric_to_asc(num2, 0));
|
|
printf("decimal = %s\n", PGTYPESnumeric_to_asc(num2, 1));
|
|
printf("decimal = %s\n", PGTYPESnumeric_to_asc(num2, 2));
|
|
|
|
PGTYPESnumeric_free(num2);
|
|
PGTYPESdecimal_free(dec);
|
|
PGTYPESnumeric_free(num);
|
|
|
|
EXEC SQL COMMIT;
|
|
EXEC SQL DISCONNECT ALL;
|
|
return 0;
|
|
}
|