xo / usql

Universal command-line interface for SQL databases
MIT License
8.81k stars 346 forks source link

numbers displayed as 1.450817032e+ #448

Open hossam-houssien opened 4 months ago

hossam-houssien commented 4 months ago

numbers displayed as "1.450817032e+" it should be displayed as 1450817.032, tried to use -P numericlocale=on, this displayed as 1,450,817.032, but we required to be displayed as 1450817.032

env

  • OS: Windows
  • usql 0.17.5
kenshaw commented 4 months ago

Thanks for reporting this. Not sure what the issue is, I'll look into it.

kenshaw commented 4 months ago

This appears to be working; can you share more information about the table/query you were using? My guess is that this was returned back not as the type you expected.

kenshaw commented 4 months ago

With / without the -P setting on the command-line:

$ usql -P numericlocale=on pg:// -c "select '1450817.032'::real"
Connected with driver postgres (PostgreSQL 16.1 (Debian 16.1-1.pgdg120+1))
   float4    
-------------
 1,450,817.0 
(1 row)

$ usql pg:// -c "select '1450817.032'::real"
Connected with driver postgres (PostgreSQL 16.1 (Debian 16.1-1.pgdg120+1))
    float4    
--------------
 1.450817e+06 
(1 row)
hossam-houssien commented 3 months ago

It is working like you showing, and like I like show earlier, but what I am asking for it should be displayed as 1450817.0 without the thousands separator

kenshaw commented 3 months ago

Compare the output for the following script:

select '1251258098.1555901285'::numeric;
select '1251258098.1555901285'::float4;
select '1251258098.1555901285'::float8;
select '1251258098.1555901285'::double precision;

With psql:

$ psql -f f.sql postgres://postgres:P4ssw0rd@localhost
        numeric        
-----------------------
 1251258098.1555901285
(1 row)

    float4     
---------------
 1.2512581e+09
(1 row)

      float8      
------------------
 1251258098.15559
(1 row)

      float8      
------------------
 1251258098.15559
(1 row)

With usql:

$ usql -f f.sql postgres://postgres:P4ssw0rd@localhost
        numeric        
-----------------------
 1251258098.1555901285 
(1 row)

    float4     
---------------
 1.2512581e+09 
(1 row)

        float8        
----------------------
 1.25125809815559e+09 
(1 row)

        float8        
----------------------
 1.25125809815559e+09 
(1 row)

The differences here are in the difference between how Go and C treat their numeric formatting. I agree Go seems to be wrong here for extremely large numbers, and I'll look into seeing if we can get something closer to psql's formatting in the future.

In the interim, I would suggest casting your number as a string, or to the numeric type, or otherwise use the database's formatting functionality. This is an extremely low priority to change/fix, mostly due to the non-trivial complexity involved.

(caveat: apologies in advance if I'm mistaken about psql just using printf or a variant of it, as I'm mostly assuming here without looking at psql's source)