dfritch
dfritch

Reputation: 359

How to use Banker's Rounding Mode in Postgresql?

I have tried to search, but I can't seem to find an implementation of Banker's Rounding Mode in Postgresql. I found this thread in which there was an effort to create an implementation in T-SQL https://www.sqlservercentral.com/Forums/Topic246556-8-1.aspx.

For example, Andrew Vasylevskyy nicely gives some examples Examples of banker's rounding (results of Math.Round in .NET):

Math.Round(3.454,2) -> 3.45
Math.Round(3.455,2) -> 3.46
Math.Round(3.445,2) -> 3.44
Math.Round(3.456,2) -> 3.46 

Based on this thread PostgreSQL round(v numeric, s int) it appears that there is not a way to configure the default round() function to use a specific mode and that it does not use Banker's Rounding Mode. I do see there is a round_half_even or a round_half_odd but it is my understanding it is not the same as Banker's rounding.

Has anyone created a function that does this or is there an extension out there that can be added to do this?

Upvotes: 2

Views: 2496

Answers (1)

Schwern
Schwern

Reputation: 165456

Wikipedia and Java say Banker's Rounding is round-half-to-even.

I don't know if a Postgres extension which implements alternative rounding, which is surprising. Best I've found is this answer with implementations as functions.

Upvotes: 1

Related Questions