Re: rounding down a number - Mailing list pgsql-novice

From Duncan Garland
Subject Re: rounding down a number
Date
Msg-id MBEPKEEDMKGCDODFKLPPKEABEGAA.duncan.garland@ntlworld.com
Whole thread Raw
In response to rounding down a number  ("Michael" <mmcelarn@hotmail.com>)
List pgsql-novice
Why not create a wrapper function? Something along the lines of:
 
CREATE OR REPLACE FUNCTION f_round( NUMERIC ) RETURNS INTEGER AS '
DECLARE
 
  original_number ALIAS FOR $1;
  rounded_number INTEGER;
 
BEGIN
 
  rounded_number := ROUND( original_number );
  IF rounded_number = original_number + 0.5 THEN
 
    rounded_number := rounded_number - 1;
 
  END IF;
 
  RETURN rounded_number;
 
END;
' LANGUAGE 'plpgsql';  
 
SELECT f_round( 100.0 ) FROM team LIMIT 1;
SELECT f_round( 100.1 ) FROM team LIMIT 1;
SELECT f_round( 100.4 ) FROM team LIMIT 1;
SELECT f_round( 100.4999999999 ) FROM team LIMIT 1;
SELECT f_round( 100.5 ) FROM team LIMIT 1;
SELECT f_round( 100.5000000001 ) FROM team LIMIT 1;
SELECT f_round( 100.6 ) FROM team LIMIT 1;
SELECT f_round( 100.9 ) FROM team LIMIT 1;
 
 f_round
---------
     100
(1 row)
 
 f_round
---------
     100
(1 row)
 
 f_round
---------
     100
(1 row)
 
 f_round
---------
     100
(1 row)
 
 f_round
---------
     100
(1 row)
 
 f_round
---------
     101
(1 row)
 
 f_round
---------
     101
(1 row)
 
 f_round
---------
     101
(1 row)
-----Original Message-----
From: pgsql-novice-owner@postgresql.org [mailto:pgsql-novice-owner@postgresql.org]On Behalf Of Michael
Sent: 30 March 2007 22:39
To: pgsql-novice@postgresql.org
Subject: [NOVICE] rounding down a number

Hi,

I’m trying to round down any number with a half, but keep the round function for all other fractions.

For example

 

 

100.1 becomes 100

100.4 becomes 100

100.5 becomes 100 (this one would ordinarily round to 101)

100.6 becomes 101

100.9 becomes 101

 

Thanks, Mick

pgsql-novice by date:

Previous
From: Frank Bax
Date:
Subject: Re: rounding down a number
Next
From: "Duncan Garland"
Date:
Subject: Re: rounding down a number