Esempio n. 1
0
Datum decimal64_round(PG_FUNCTION_ARGS)
{
        PGDecimal64 a = PG_GETARG_DECIMAL64(0);
        int32_t scale = PG_GETARG_INT32(1);
        PGDecimal64 ret;

        static PGDecimal64* pquan;
        static PGDecimal64 quan[16];

        DECLARE_DEC_CTXT(DEC_INIT_DECIMAL64);

        if (pquan == 0) {
                int i;
                PGDecimal64 ten;
                decDoubleFromInt32(&ten, 10);
                decDoubleFromInt32(&quan[0], 1);
                for (i = 1; i < 16; i++) {
                        decDoubleDivide(&quan[i], &quan[i-1], &ten, &dc);
                }
                pquan = &quan[0];
        }

        if (scale < 0 || scale > 15) {
                elog(ERROR, "decimal32 rounding to an invalid scale");
        }

        decDoubleQuantize(&ret, &a, &quan[scale], &dc);
        PG_RETURN_DECIMAL64(ret);
}
Esempio n. 2
0
Decimal64 DecimalUtil::quantize(Decimal64 value, Decimal64 exponent)
{
    Decimal64 result = value;
    decDoubleQuantize(result.data(),
                      value.data(),
                      exponent.data(),
                      getContext());
    return result;
}