Обсуждение: Decimal ??

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

Decimal ??

От
Tubagus Nizomi
Дата:
I have table :
create table test (name text, price numeric(6,2));
CREATE
insert into test values ('John',0.75);
INSERT

select * from test;
name | price
---------
John |  0.75 

but when i insert into test values ('John',3/4);
INSERT

select * from test;
name | price
---------
John |  0.00   --->  ??? 

why this value 0.00 but i want to 0.75 ??? help me please.

i am using mandrake 7 and postgres 6.5.3

Thanks
Nizomi 


Re: Decimal ??

От
Jason Earl
Дата:
In PostgreSQL 7.0 (and I would bet 6.5) if you cast
either the numerator or the denominator as a numeric
then it does what you would expect.

For example:

insert into test (name, price) values ('Jason',
3::numeric/4);

OR

insert into test (name, price) values ('Earl',
3/4::numeric);

Will do what you want.  Unfortunately:

insert into test (name, price) values ('Foobar',
(3/4)::numeric);

does _not_ do what you would expect.  This (I imagine)
is because PostgreSQL sees the two values, assumes
they are integers and does integer division.  Without
knowing exactly what you are trying to do (and what
language you are trying to do it in) it is hard to
guess if this is helpful, so write back if you need
more help.

Hopefully this is helpful.

Jason

--- Tubagus Nizomi <nizomi@dnet.net.id> wrote:
> I have table :
> create table test (name text, price numeric(6,2));
> CREATE
> insert into test values ('John',0.75);
> INSERT
>
> select * from test;
> name | price
> ---------
> John |  0.75
>
> but when i insert into test values ('John',3/4);
> INSERT
>
> select * from test;
> name | price
> ---------
> John |  0.00   --->  ???
>
> why this value 0.00 but i want to 0.75 ??? help me
> please.
>
> i am using mandrake 7 and postgres 6.5.3
>
> Thanks
> Nizomi


__________________________________________________
Do You Yahoo!?
Send instant messages & get email alerts with Yahoo! Messenger.
http://im.yahoo.com/


Re: Decimal ??

От
Tom Lane
Дата:
Tubagus Nizomi <nizomi@dnet.net.id> writes:
> I have table :
> create table test (name text, price numeric(6,2));
> but when i insert into test values ('John',3/4);
> why this value 0.00 but i want to 0.75 ??? help me please.

Offhand I'd bet that "3/4" is being interpreted as an integer
division expression.  Try "3.0/4.0" or some such if you want
a fractional result...
        regards, tom lane