2 years ago

#46203

test-img

Daniel

Float precision issue - different values then from pgAdmin

I develop a Java project and running into a problem with incorrect text data value from the PostgreSQL database.

For example, in PostgreSQL I have a table with text type columns. Inside that table, I have a text value of 62.1 or something similar. When retrieving data with jdbcTemplate (query or queryForList method) the result is 62.09999999999999996.

When I use ResultSet getFloat (index) it works fine. But I need a different solution as my database is huge and the tables contain different values ​​on different column indexes. So if I get stuck with the getFloat (index) method then I will have to implement a different approach for each table.

Code Example. Database pocedure:

    CREATE OR REPLACE FUNCTION public.float_test_ret_cast_table2(
    )
    RETURNS TABLE(value text, value2 character varying, value3 text, value4 text, value5 text, value6 text) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
DECLARE
BEGIN
    DROP TABLE IF EXISTS tmp_float_cast_test2;
        EXECUTE 'CREATE TABLE tmp_float_cast_test2 AS ( 
            SELECT value, value2, value3::text, value4::text, value5::text, (value6::real / 100)::text FROM "FloatTest"    
        )';
    RETURN QUERY SELECT * FROM tmp_float_cast_test2;
END;
$BODY$;

Query results:

  • from pg admin:
select * from float_test_ret_cast_table2()
62.1 | 62,1 | 62.1111 | 62.1111 | 62.1111 | 6.21

from java:

select * from float_test_ret_cast_table2()
62.1 | 62,1 | 62.1111000000000004 | 62.1110992 | 62.1111 | 6.20999999999999996

How can I fix this problem?

java

postgresql

precision

jdbctemplate

0 Answers

Your Answer

Accepted video resources