floating point - PostgreSQL round(v numeric, s int) -
which method postgres round(v numeric, s int)
use?
- round half up
- round half down
- round half away zero
- round half towards zero
- round half even
- round half odd
i'm looking documentation reference.
sorry, don't see hint of in documentation, a @ code indicates it's using round half away zero; carry
added digits
, thereby increasing absolute value of variable, regardless of sign
is. simple experiment (psql 9.1) confirms this:
test=# create table nvals (v numeric(5,2)); create table test=# insert nvals (v) values (-0.25), (-0.15), (-0.05), (0.05), (0.15), (0.25); insert 0 6 test=# select v, round(v, 1) nvals; v | round -------+------- -0.25 | -0.3 -0.15 | -0.2 -0.05 | -0.1 0.05 | 0.1 0.15 | 0.2 0.25 | 0.3 (6 rows)
interesting, because round(v dp)
uses half even:
test=# create table vals (v double precision); create table test=# insert vals (v) values (-2.5), (-1.5), (-0.5), (0.5), (1.5), (2.5); insert 0 6 test=# select v, round(v) vals; v | round ------+------- -2.5 | -2 -1.5 | -2 -0.5 | -0 0.5 | 0 1.5 | 2 2.5 | 2 (6 rows)
the latter behavior platform-dependent, since looks it uses rint(3) under hood.
you implement different rounding scheme if necessary. see tometzky's answer examples.
Comments
Post a Comment