It is not documented, so it can change.
Here is my round_half_even(numeric,integer) :
create or replace function round_half_even(val numeric, prec integer) returns numeric as $$ declare retval numeric; difference numeric; even boolean; begin retval := round(val,prec); difference := retval-val; if abs(difference)*(10::numeric^prec) = 0.5::numeric then even := (retval * (10::numeric^prec)) % 2::numeric = 0::numeric; if not even then retval := round(val-difference,prec); end if; end if; return retval; end; $$ language plpgsql immutable strict;
And round_half_odd(numeric,integer) :
create or replace function round_half_odd(val numeric, prec integer) returns numeric as $$ declare retval numeric; difference numeric; even boolean; begin retval := round(val,prec); difference := retval-val; if abs(difference)*(10::numeric^prec) = 0.5::numeric then even := (retval * (10::numeric^prec)) % 2::numeric = 0::numeric; if even then retval := round(val-difference,prec); end if; end if; return retval; end; $$ language plpgsql immutable strict;
They manage about 500,000 calls per second, 6 times slower than the standard round(numeric,integer) . They also work for zero and for negative accuracy.
source share