python mysql connector adds unnecessary precision value to a float data type cell having an integer value

145 views Asked by At

I have a table in which there is a column "column_float" which is of data type float but the value inserted for this column is an integer value 5

When I read this value through python mysql connector it gives me a float value 5.0

My requirement is that if an integer value is there in the float column it should be read as an integer only without adding any extra precision.

Any help here ? Any settings for python mysql connector to do so.

1

There are 1 answers

2
nbk On

Floating point have always decimals, but you can check if it has any and then round only when there is only a zero.

Replace the @a with your column name

SET @a = 5.0;
SELECT IF( ROUND(@a,0) = @a,ROUND(@a,0),@a)

Result

# IF( ROUND(@a,0) = @a,ROUND(@a,0),@a)
5

And form any other like

SET @a = 5.1;
SELECT IF( ROUND(@a,0) = @a,ROUND(@a,0),@a)

Result

# IF( ROUND(@a,0) = @a,ROUND(@a,0),@a)
5.1

For python there a there same functions

round(5.0)
# Returns: 5