Thread: Rounding Float Array

Rounding Float Array

From
Alex Magnum
Date:
Hello,

I have a float array holding geo location information.

              geoloc
-----------------------------------
 {5.3443133704554,100.29457569122}
 {5.3885574294704,100.29601335526}
 {3.1654978750403,101.60915851593}
 {5.3766154817748,100.31472444534}
 {3.1545014704258,101.70036971569}
(5 rows)

Is there an easy way to round all values to 4 decimals.

I can round the individual values and return them seperately but I need to return them as an array. 

  lat    |   long
---------+-----------
 5.34431 | 100.29458
 5.38856 | 100.29601
 3.16550 | 101.60916
 5.37662 | 100.31472
 3.15450 | 101.70037
(5 rows)

Any suggestion is highly appreciated.

Thanks
Alex

Re: Rounding Float Array

From
Chris Mair
Date:
On 21/09/15 11:55, Alex Magnum wrote:
> Hello,
>
> I have a float array holding geo location information.
>
>              geoloc
> -----------------------------------
>  {5.3443133704554,100.29457569122}
>  {5.3885574294704,100.29601335526}
>  {3.1654978750403,101.60915851593}
>  {5.3766154817748,100.31472444534}
>  {3.1545014704258,101.70036971569}
> (5 rows)
>
> Is there an easy way to round all values to 4 decimals.
>
> I can round the individual values and return them seperately but I need to return them as an array.
>
>  lat    |   long
> ---------+-----------
>  5.34431 | 100.29458
>  5.38856 | 100.29601
>  3.16550 | 101.60916
>  5.37662 | 100.31472
>  3.15450 | 101.70037
> (5 rows)
>
> Any suggestion is highly appreciated.


This might work for you.

Bye,
Chris.

chris=# select * from geoloc;
              geoloc
-----------------------------------
 {5.3443133704554,100.29457569122}
 {5.3885574294704,100.29601335526}
 {3.1654978750403,101.60915851593}
 {5.3766154817748,100.31472444534}
 {3.1545014704258,101.70036971569}
(5 rows)

chris=# select (select array_agg(to_char(x, '999.9999')::float) from unnest(geoloc) as x) from geoloc;;
     array_agg
-------------------
 {5.3443,100.2946}
 {5.3886,100.296}
 {3.1655,101.6092}
 {5.3766,100.3147}
 {3.1545,101.7004}
(5 rows)






Re: Rounding Float Array

From
Andreas Kretschmer
Date:
Alex Magnum <magnum11200@gmail.com> wrote:

> Hello,
>
> I have a float array holding geo location information.
>
>               geoloc
> -----------------------------------
>  {5.3443133704554,100.29457569122}
>  {5.3885574294704,100.29601335526}
>  {3.1654978750403,101.60915851593}
>  {5.3766154817748,100.31472444534}
>  {3.1545014704258,101.70036971569}
> (5 rows)
>
> Is there an easy way to round all values to 4 decimals.

sure:

test=*# select * from alex ;
              geoloc
-----------------------------------
 {5.3443133704554,100.29457569122}
 {5.3885574294704,100.29601335526}
 {3.1654978750403,101.60915851593}
 {5.3766154817748,100.31472444534}
(4 rows)

Time: 0,245 ms
test=*# update alex set geoloc = array[round(geoloc[1]::numeric,4),
round(geoloc[2]::numeric,4)];
UPDATE 4
Time: 0,454 ms
test=*# select * from alex ;
      geoloc
-------------------
 {5.3443,100.2946}
 {5.3886,100.296}
 {3.1655,101.6092}
 {5.3766,100.3147}
(4 rows)




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."   (unknown)
Kaufbach, Saxony, Germany, Europe.              N 51.05082°, E 13.56889°


Re: Rounding Float Array

From
hubert depesz lubaczewski
Date:
On Mon, Sep 21, 2015 at 11:55:23AM +0200, Alex Magnum wrote:
> Hello,
>
> I have a float array holding geo location information.
>
>               geoloc
> -----------------------------------
>  {5.3443133704554,100.29457569122}
>  {5.3885574294704,100.29601335526}
>  {3.1654978750403,101.60915851593}
>  {5.3766154817748,100.31472444534}
>  {3.1545014704258,101.70036971569}
> (5 rows)
> Is there an easy way to round all values to 4 decimals.

Sure:
$ select geoloc from alex;
              geoloc
-----------------------------------
 {5.3443133704554,100.29457569122}
 {5.3885574294704,100.29601335526}
 {3.1654978750403,101.60915851593}
 {5.3766154817748,100.31472444534}
 {3.1545014704258,101.70036971569}
(5 rows)

(depesz@[local]:5960) 12:15:46 [depesz]
$ select geoloc::numeric(8,4)[] from alex;
      geoloc
-------------------
 {5.3443,100.2946}
 {5.3886,100.2960}
 {3.1655,101.6092}
 {5.3766,100.3147}
 {3.1545,101.7004}
(5 rows)

depesz


Re: Rounding Float Array

From
Andreas Kretschmer
Date:
hubert depesz lubaczewski <depesz@depesz.com> wrote:

> > Is there an easy way to round all values to 4 decimals.
>
> Sure:
>
> (depesz@[local]:5960) 12:15:46 [depesz]
> $ select geoloc::numeric(8,4)[] from alex;
>       geoloc
> -------------------
>  {5.3443,100.2946}

Nice!


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."   (unknown)
Kaufbach, Saxony, Germany, Europe.              N 51.05082°, E 13.56889°


Re: Rounding Float Array

From
Chris Mair
Date:
>> Sure:
>>
>> (depesz@[local]:5960) 12:15:46 [depesz]
>> $ select geoloc::numeric(8,4)[] from alex;
>>       geoloc
>> -------------------
>>  {5.3443,100.2946}
>
> Nice!

Indeed :)

Bye,
Chris.