Datum decimal64_numeric(PG_FUNCTION_ARGS) { PGDecimal64 a = PG_GETARG_DECIMAL64(0); Datum ret; char s[DECDOUBLE_String]; decDoubleToString(&a, s); ret = DirectFunctionCall3(numeric_in, CStringGetDatum(s), Int32GetDatum(0), Int32GetDatum(-1)); PG_RETURN_DATUM(ret); }
Datum decimal64_out(PG_FUNCTION_ARGS) { PGDecimal64 d = PG_GETARG_DECIMAL64(0); char *s = (char *) palloc(DECDOUBLE_String); if (!s) { elog(ERROR, "OOM"); } decDoubleToString(&d, s); PG_RETURN_CSTRING(s); }
Datum decimal64_float8(PG_FUNCTION_ARGS) { PGDecimal64 a = PG_GETARG_DECIMAL64(0); char s[DECDOUBLE_String]; char *endp = 0; double d; decDoubleToString(&a, s); errno = 0; d = strtod(s, &endp); if (endp == s || errno != 0) { elog(ERROR, "cannot convert decimal64 to float8"); } PG_RETURN_FLOAT8(d); }
int testDecDouble() { int i; decDouble a, b; decContext set; char string[DECDOUBLE_String]; decContextDefault(&set, DEC_INIT_DECDOUBLE); // initialize decDoubleFromString(&a, "0.13", &set); decDoubleFromString(&b, "8.7", &set); for (i = 0; i < 10; i++) decDoubleAdd(&b, &a, &b, &set); // b += a; decDoubleToString(&b, string); printf("dec064: 8.7 + 0.13 * 10 => %s\n", string); return 0; }