Re: numeric to text (7.3) - Mailing list pgsql-hackers

From Joel Burton
Subject Re: numeric to text (7.3)
Date
Msg-id 20021202195027.GA14132@temp.joelburton.com
Whole thread Raw
In response to numeric to text (7.3)  (Szima Gábor <sygma@tesla.hu>)
List pgsql-hackers
On Mon, Dec 02, 2002 at 08:23:24PM +0100, Szima Gábor wrote:
> OK, but:
> 
> template1=# select round('2.001'::numeric);
>  round
> -------
>      2
> (1 row)
> 
> template1=# select round('2.001'::numeric,2);
>  round
> -------
>   2.00
> (1 row)
> 
> 
> The good idea (in 7.2):
> 
> template1=# select text('2.000'::numeric);
>  text
> ------
>  2
> (1 row)
> 
> template1=# select text('2.001'::numeric);
>  text
> -------
>  2.001
> (1 row)
> 
> 
> This feature is missing from 7.3..

Not sure I'd call it a feature -- ISTM that text(numeric) should show
all the precision you gave it, and not shave it down to the
least-precise number that is still equal.

Anyhoo, you can get what you want with some ugly-but-straightforward
trimming:

(in 7.3):

joel@joel=# select rtrim(rtrim('2.000'::numeric, '0'),'.');rtrim
-------2
(1 row)

joel@joel=# select rtrim(rtrim('2.001'::numeric, '0'),'.');rtrim
-------2.001
(1 row)


Easy enough to make this into a function trim_as_much(numeric) or
somesuch.
   
-- 

Joel BURTON  |  joel@joelburton.com  |  joelburton.com  |  aim: wjoelburton
Independent Knowledge Management Consultant


pgsql-hackers by date:

Previous
From: "Magnus Naeslund(f)"
Date:
Subject: Re: 7.4 Wishlist
Next
From: Henner Zeller
Date:
Subject: PG 7.3: Query Meta Data with the JDBC-driver