floating point - PostgreSQL round(v numeric, s int) -


which method postgres round(v numeric, s int) use?

  1. round half up
  2. round half down
  3. round half away zero
  4. round half towards zero
  5. round half even
  6. 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

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -