Thread: decimal_part() function

decimal_part() function

From
Sferacarta Software
Date:
Hi all,

I'm looking for a function similar to dtrunc() to return the decimal
part of a float.

dtrunc(12.34) returns 12, I need something that returns 34

Does anyone knows how to do this thing?
Thanks
-Jose'-



Re: [GENERAL] decimal_part() function

From
Herouth Maoz
Date:
At 17:46 +0200 on 15/12/98, Sferacarta Software wrote:


> I'm looking for a function similar to dtrunc() to return the decimal
> part of a float.
>
> dtrunc(12.34) returns 12, I need something that returns 34

Here is a table having one float column named k. % is the truncation
operator (I suppose it's the same as dtrunc). so... k - %k gives you only
the fraction:

testing=> select k, %k, k - %k from test1;
    k|?column?|          ?column?
-----+--------+------------------
  1.8|       1|               0.8
 3.78|       3|              0.78
  0.4|       0|               0.4
  1.8|       1|               0.8
  0.4|       0|               0.4
 9.24|       9|              0.24
    6|       6|                 0
 9.24|       9|              0.24
  1.1|       1|               0.1
 -1.8|      -1|              -0.8
-10.2|     -10|-0.199999999999999
(11 rows)

If that's what you expect in the negative numbers, you're home free. If
not, you can use some other operator combination.

Herouth

--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma



Re[2]: [GENERAL] decimal_part() function

From
Sferacarta Software
Date:
Hello Herouth,

martedì, 15 dicembre 98, you wrote:

HM> At 17:46 +0200 on 15/12/98, Sferacarta Software wrote:


>> I'm looking for a function similar to dtrunc() to return the decimal
>> part of a float.
>>
>> dtrunc(12.34) returns 12, I need something that returns 34

HM> Here is a table having one float column named k. % is the truncation
HM> operator (I suppose it's the same as dtrunc). so... k - %k gives you only
HM> the fraction:

testing=>> select k, %k, k - %k from test1;
HM>     k|?column?|          ?column?
HM> -----+--------+------------------
HM>   1.8|       1|               0.8
HM>  3.78|       3|              0.78
HM>   0.4|       0|               0.4
HM>   1.8|       1|               0.8
HM>   0.4|       0|               0.4
HM>  9.24|       9|              0.24
HM>     6|       6|                 0
HM>  9.24|       9|              0.24
HM>   1.1|       1|               0.1
HM>  -1.8|      -1|              -0.8
HM> -10.2|     -10|-0.199999999999999
HM> (11 rows)

HM> If that's what you expect in the negative numbers, you're home free. If
HM> not, you can use some other operator combination.
HM> Herouth

I can't use this method because this has problems with precision, I
found this trick to have the decimal part with 3 decimal places...

prova=> select k, % k, k - %k, dround(date_part('millisecond',k)) from test;
      k|?column?|         ?column?|dround
-------+--------+-----------------+------
 123.45|     123|0.450000000000003|   450
123.101|     123|0.100999999999999|   101
 12.201|      12|0.201000000000001|   201
(3 rows)

Thanks any way.
-Jose'-