Обсуждение: reformatting floats ?

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

reformatting floats ?

От
"Gauthier, Dave"
Дата:

Hi:

 

if...  

create table coords (id int, x float, y float);

then... 

insert into coords (id,x,y) values (1,1.000,2.001)

and then...

            select * from coords

i get...

            1,1,2.001

i want...

            1.1.000,2.001

while retaining the numeric nature of the x,y data (for math ops in other operations).

 

How can I reformat the float output (sort of like using “%5.3f” in good-ole C)

 

Thanks !

Re: reformatting floats ?

От
Martijn van Oosterhout
Дата:
On Thu, Dec 06, 2007 at 01:22:55PM -0500, Gauthier, Dave wrote:
> i get...
>
>             1,1,2.001
>
> i want...
>
>             1.1.000,2.001
>
> while retaining the numeric nature of the x,y data (for math ops in
> other operations).

I imagine you want to_char().

Have a nice day,
--
Martijn van Oosterhout   <kleptog@svana.org>   http://svana.org/kleptog/
> Those who make peaceful revolution impossible will make violent revolution inevitable.
>  -- John F Kennedy

Вложения

Re: reformatting floats ?

От
Andreas Kretschmer
Дата:
Gauthier, Dave <dave.gauthier@intel.com> schrieb:

>
>
> Hi:
>
> if...
> create table coords (id int, x float, y float);
> then...
> insert into coords (id,x,y) values (1,1.000,2.001)
> and then...
>             select * from coords
> i get...
>             1,1,2.001
> i want...
>             1.1.000,2.001
> while retaining the numeric nature of the x,y data (for math ops in other
> operations).
>
> How can I reformat the float output (sort of like using %5.3f in good-ole C)

CAST it to numeric ;-)

test=# create table coords (id int, x float, y float);
CREATE TABLE
Time: 4.437 ms
test=*# insert into coords (id,x,y) values (1,1.000,2.001);
INSERT 0 1
Time: 4.831 ms
test=*# select id, x::numeric(10,3), y::numeric(10,3) from coords;
 id |   x   |   y
----+-------+-------
  1 | 1.000 | 2.001


Andreas
--
Really, I'm not out to destroy Microsoft. That will just be a completely
unintentional side effect.                              (Linus Torvalds)
"If I was god, I would recompile penguin with --enable-fly."    (unknow)
Kaufbach, Saxony, Germany, Europe.              N 51.05082°, E 13.56889°

Re: reformatting floats ?

От
"Gauthier, Dave"
Дата:
Both work (to_char and casting to numeric)
Thanks !

-----Original Message-----
From: pgsql-general-owner@postgresql.org [mailto:pgsql-general-owner@postgresql.org] On Behalf Of Andreas Kretschmer
Sent: Thursday, December 06, 2007 1:46 PM
To: pgsql-general@postgresql.org
Subject: Re: [GENERAL] reformatting floats ?

Gauthier, Dave <dave.gauthier@intel.com> schrieb:

>
>
> Hi:
>
> if...
> create table coords (id int, x float, y float);
> then...
> insert into coords (id,x,y) values (1,1.000,2.001)
> and then...
>             select * from coords
> i get...
>             1,1,2.001
> i want...
>             1.1.000,2.001
> while retaining the numeric nature of the x,y data (for math ops in other
> operations).
>
> How can I reformat the float output (sort of like using %5.3f in good-ole C)

CAST it to numeric ;-)

test=# create table coords (id int, x float, y float);
CREATE TABLE
Time: 4.437 ms
test=*# insert into coords (id,x,y) values (1,1.000,2.001);
INSERT 0 1
Time: 4.831 ms
test=*# select id, x::numeric(10,3), y::numeric(10,3) from coords;
 id |   x   |   y
----+-------+-------
  1 | 1.000 | 2.001


Andreas
--
Really, I'm not out to destroy Microsoft. That will just be a completely
unintentional side effect.                              (Linus Torvalds)
"If I was god, I would recompile penguin with --enable-fly."    (unknow)
Kaufbach, Saxony, Germany, Europe.              N 51.05082°, E 13.56889°

---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
       choose an index scan if your joining column's datatypes do not
       match