Datum decimal128_decimal64(PG_FUNCTION_ARGS) { PGDecimal128* pa = PG_GETARG_P_DECIMAL128(0); PGDecimal64 ret; DECLARE_DEC_CTXT(DEC_INIT_DECIMAL64); decDoubleFromWider(&ret, pa, &dc); DEC_CHK_STATUS(dc, DEC_STATUS_OFUF, "decimal128 to decimal64 over/under flow"); PG_RETURN_DECIMAL64(ret); }
/* Use decNumber to convert directly from _Decimal128 to _Decimal64. */ _Decimal64 DFP_TO_DFP (_Decimal128 f_from) { union { _Decimal64 c; decDouble f; } to; union { _Decimal128 c; decQuad f; } from; decContext context; decContextDefault (&context, CONTEXT_INIT); DFP_INIT_ROUNDMODE (context.round); from.c = f_from; to.f = *decDoubleFromWider (&to.f, &from.f, &context); if (DFP_EXCEPTIONS_ENABLED && context.status != 0) dfp_conversion_exceptions (context.status); return to.c; }