Re: numeric rounding

From: "James Moe" <jimoe(at)sohnen-moe(dot)com>
To: "Postgresql General Mail List" <pgsql-general(at)postgresql(dot)org>
Subject: Re: numeric rounding
Date: 2003-09-29 18:10:05
Message-ID: auto-000000650502@sohnen-moe.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Mon, 29 Sep 2003 00:47:05 -0700 (PDT), Gezeala 'Eyah' \"Bacu±o\" II wrote:

>i have a plpgsql function where in i compute numeric values for my php scripts..
>my problem is my function just won't round some numbers properly.
>
What do you mean by "properly?" How, exactly, do you want the numbers rounded? What
about negative numbers?
There is a round() function that offers various options. I do not know how it handles
negative numbers, though.
For money numbers I found this to be a reasonable method:

sign = (0.0 <= N) ? 1 : -1;
tmp = abs(N);
tmp = round((tmp + 0.005), 2);
N = tmp * sign;

- --
jimoe at sohnen-moe dot com
pgp/gpg public key: http://www.keyserver.net/en/
-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 5.0 OS/2 for non-commercial use
Comment: PGP 5.0 for OS/2
Charset: cp850

wj8DBQE/eHV9sxxMki0foKoRAnitAJ4yd1LG4eBlNpsQL3tFLqera9JRQACg3rKX
LnDwRQkyga6uxBzeEjEX+qE=
=LIzc
-----END PGP SIGNATURE-----

Browse pgsql-general by date

  From Date Subject
Next Message Robert Treat 2003-09-29 18:19:47 Re: Postgres vs. Progress performance
Previous Message Tom Lane 2003-09-29 17:51:59 Re: numeric rounding