PostgreSQL 回合(v 数字,s int)

标签 postgresql floating-point rounding

哪个method Postgres round(v numeric, s int)有什么用?

  1. 四舍五入
  2. 下半场
  3. 从零开始四舍五入
  4. 向零舍入一半
  5. 四舍五入
  6. 四舍五入为奇数

我正在寻找文档引用。

最佳答案

它没有记录,所以它可以改变。

这是我的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;

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;

他们每秒处理大约 500000 次调用,比标准 round(numeric,integer) 慢 6 倍。它们也适用于零精度和负精度。

关于PostgreSQL 回合(v 数字,s int),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22613702/

相关文章:

arrays - WHERE 子句中的 Postgres 数组查找

ruby - Ruby 2.2.0 中的混编比 2.1.5 慢

r - summary() 舍入

Java 数字框架

ios - 将大数除以 1000 并四舍五入到小数点后一位

sql - 我无法弄清楚 v_out 和 v_in 在 SQL 语句中的含义

postgresql - 如何在 postgres 中编写组合函数?

json - 在一列中获取所有 jsonb 值以供在 to_tsvector 中使用

postgresql 四舍五入函数

c - 为什么带有 %.2g 的 printf 在点后不显示两位数?