Subject RE: [firebird-support] Bankers Rounding
Author Svein Erling Tysvær
You could try something like

select case
when
(cast(MyField as Integer)/2)*2 <> cast(MyField as Integer) //Does Firebird round to odd number?
and cast(MyField * 2 as Integer) = MyField * 2 //And is it exactly 0.5?
then
case
when MyField > 0
then cast(MyField - 0.1 as Integer) //Round down positive numbers
else cast(MyField + 0.1 as Integer) //Round up negative numbers
end
else cast(MyField as Integer) //Just let Firebird round as usual
end
from MyTable

This should round down 2.45, 2.5 and 3.45, but round up 2.55, 3.5 and 3.55. Though don't expect this to work with real numbers (as opposed to decimal numbers).

And a little disclaimer: The algorithm above isn't taken from any book or program, it just popped up in my fragile, error-prone mind when reading Thomas' question. Hence, it should be tested thoroughly before being trusted.

HTH,
Set

-----Original Message-----
From: firebird-support@yahoogroups.com [mailto:firebird-support@yahoogroups.com] On Behalf Of Thomas Clarke
Sent: 21. november 2007 14:13
To: firebird-support@yahoogroups.com
Subject: RE: [firebird-support] Bankers Rounding

Apologies the Firebird version is 2.0.3.

Thomas.

_____

Hello to everyone,

I have the following challenge:

I am manipulating data extracted from an Informix 10 database using Java,
JayBird and Firebird 2.1. The output file is going to be imported into an
ACCPAC accounting application. The challenge I face is that ACCPAC expects
the numbers to be rounded using Bankers Rounding
(http://www.cis
<http://www.cis.udel.edu/~breech/contest/problems/bankers-rounding.html>
udel.edu/~breech/contest/problems/bankers-rounding.html).
Does anyone know of a UDF or a technique that can provide this type of
rounding in Firebird?

Thanks.

Thomas Clarke.