Обсуждение: rounding down a number

Поиск
Список
Период
Сортировка

rounding down a number

От
"Michael"
Дата:

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

Re: rounding down a number

От
Phillip Smith
Дата:
Subtract .1 from the number before rounding it when you want it to round that way...

100.5 will become 100.4 and round down.
100.6 will become 100.5 and round up.
Everything else will round as 'normal'.

~p


On Fri, 2007-03-30 at 14:38 -0700, Michael wrote:
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



*******************Confidentiality and Privilege Notice*******************

The material contained in this message is privileged and confidential to the addressee. If you are not the addressee indicated in this message or responsible for delivery of the message to such person, you may not copy or deliver this message to anyone, and you should destroy it and kindly notify the sender by reply email.

Information in this message that does not relate to the official business of Weatherbeeta must be treated as neither given nor endorsed by Weatherbeeta. Weatherbeeta, its employees, contractors or associates shall not be liable for direct, indirect or consequential loss arising from transmission of this message or any attachments

Re: rounding down a number

От
"Jim Stalewski"
Дата:
Try subtracting .01 prior to passing the number to the rounding
function.

________________________________________
From: pgsql-novice-owner@postgresql.org
[mailto:pgsql-novice-owner@postgresql.org] On Behalf Of Michael
Sent: Friday, March 30, 2007 4:39 PM
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


This email and any files transmitted with it are confidential and intended solely for the use of the individual or
entityto whom they are addressed. If you have received this email in error please notify the sender and delete it.
Pleasenote that any views or opinions presented in this email are solely those of the author and do not necessarily
representthose of the company.  
No employee or agent is authorized to conclude any binding agreement on behalf of Visa Lighting with another party by
emailwithout express written confirmation by an authorized representative of the Company. 
Finally, the recipient should check this email and any attachments for the presence of viruses. The company accepts no
liabilityfor any damage caused by any virus transmitted by this email.  




Re: rounding down a number

От
Frank Bax
Дата:
100.51 will become 100.41 and round down to 100 - Are all input only one
decimal place?



At 05:56 PM 3/30/07, Phillip Smith wrote:

>Subtract .1 from the number before rounding it when you want it to round
>that way...
>
>100.5 will become 100.4 and round down.
>100.6 will become 100.5 and round up.
>Everything else will round as 'normal'.
>
>~p
>
>
>On Fri, 2007-03-30 at 14:38 -0700, Michael wrote:
>>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


Re: rounding down a number

От
"Duncan Garland"
Дата:
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

Re: rounding down a number

От
"Duncan Garland"
Дата:
What behaviour are you expecting for negative numbers? This is what my function does:
 
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
---------
    -101
(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