Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Float values to String in Postgresql

I'm using PgSQL 9.1.

SELECT 5.1::text 

is working fine with output '5.1' in PgAdmin3, but with JDBC Prepared statement EXEC SQL, the result always as '5.0999999'.

Of source SELECT to_char(5.1, '9.9') will work, however this is not what I want. I want to get '5' in the case of 5.0 without decimal point, so I just stay on the ::text conversion.

My assumption is there might be some session environment settings affected to this conversion somewhere, but I can't figure out how to find.

I know this is just a silly question... please help.

like image 742
tsohr Avatar asked Mar 17 '23 00:03

tsohr


1 Answers

I think I found a solution. Try to cast into numeric type first...

SELECT (5.1::numeric)::text 

Please add your answer if you have a better solution.

like image 177
tsohr Avatar answered Mar 23 '23 07:03

tsohr