Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

OT: Retrieve float number from mysql db

by Anonymous Monk
on Oct 16, 2015 at 11:48 UTC ( [id://1145098]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a field in my mysql table type float When I read it with

my $sth = $dbh->prepare("SELECT filed FROM table WHERE id = 1");

I get the value 1679970

when I check the database

"SELECT filed FROM table WHERE id = 1"

I also get 1679970

But when I check it with

"SELECT format(filed,3) FROM table WHERE id = 1"

I get 1679971.000

I do know that the value is in fact 1679971 because I inserted the record myself

And if I retrieve the value with a php script I get 1679971

Any idea why? How can I get the right value 1679971?

  • Comment on OT: Retrieve float number from mysql db

Replies are listed 'Best First'.
Re: Retrieve float number from mysql db
by hippo (Bishop) on Oct 16, 2015 at 12:43 UTC

    This is due to an error in precision when you constructed the table:

    use strict; use warnings; use Test::More tests => 10; use DBI; my $dbuser = shift @ARGV || 'foo'; my $dbpass = shift @ARGV || 'bar'; my $val = 1679971; my $dbh = DBI->connect ('dbi:mysql:test', $dbuser, $dbpass) or die "Conn error: $DBI::errstr"; ok ($dbh->do('CREATE TABLE foo ( bar FLOAT )'), 'Table created'); try_now ($dbh, $val); $dbh->do ('DROP TABLE foo'); ok ($dbh->do('CREATE TABLE foo ( bar FLOAT (10,0) )'), 'Table created +with right precision'); try_now ($dbh, $val); $dbh->do ('DROP TABLE foo'); $dbh->disconnect; sub try_now { my ($dbh, $val) = @_; my $out = 0; ok ($dbh->do('INSERT INTO foo VALUE (?)', undef, $val), 'Value + inserted'); ok (my $sth = $dbh->prepare ('SELECT bar FROM foo'), 'Select p +repared'); ok ($sth->execute, 'Select executed'); ($out) = $sth->fetchrow_array; is ($val, $out, "Output value $out equals input value $val"); }

    Here's my result of running this:

    1..10 ok 1 - Table created ok 2 - Value inserted ok 3 - Select prepared ok 4 - Select executed not ok 5 - Output value 1679970 equals input value 1679971 # Failed test 'Output value 1679970 equals input value 1679971' # at myfloat.t line 30. # got: '1679971' # expected: '1679970' ok 6 - Table created with right precision ok 7 - Value inserted ok 8 - Select prepared ok 9 - Select executed ok 10 - Output value 1679971 equals input value 1679971 # Looks like you failed 1 test of 10.

    So, it's nothing to do with Perl and shows that your PHP script is telling porkies.

    Update: I suggest you read all about FLOAT in MySQL and particularly the caveats about rounding, precision and exact comparison.

      Wikipedia also has more details on the limits of precision of 4 byte floats: Single Precision Floating Point. You can only store integers with less than 6 significant digits of precision with the guarantee that you won't suffer approximation. If you need the precision, make sure you choose a type that supports it, and be aware that floating point can get approximated different ways in different implementations that both comply with the same standard.

Re: OT: Retrieve float number from mysql db
by choroba (Cardinal) on Oct 16, 2015 at 11:58 UTC
    How did you insert the record? What's the column type in the database?
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      insert into table (field) value (1679971) and data type float
Re: OT: Retrieve float number from mysql db
by flexvault (Monsignor) on Oct 16, 2015 at 12:30 UTC

    Dear Monks,

    I know little about 'mysql' but what does the following do:

    "SELECT format(filed,3) FROM table WHERE id = 1"
    you state:
    I get 1679971.000
    That looks to me that 'format(filed,3)' is formating the number to 3 decimal places. What happens if you change it to 'format(filed,0)'?

    Hope it helps!

    Regards...Ed

    "Well done is better than well said." - Benjamin Franklin

Re: OT: Retrieve float number from mysql db
by poj (Abbot) on Oct 16, 2015 at 12:50 UTC
    Try SELECT filed+0 FROM table WHERE id = 1
    poj
Re: OT: Retrieve float number from mysql db
by Anonymous Monk on Oct 16, 2015 at 12:01 UTC

    I don't have any answers to you, but only my curiosity ...

    • what is the data engine type for the database?
    • how was the table created (the create table statement)?
    • what are the options set for the database & the table, if any?

      It is a mysql database

      create table mytable (field float);

      insert into mytable (field) value (1679971) ;

      Mysql is not the problem I think, because if I use php I get 1679971 but with perl i get 1679970

      It has to be the way perl represents the float value

      s/to/for/;

        s/Anonymous Monk/Registered User/; allows you to correct and update your posts without cluttering up the place with s/to/for/; nodes.


        Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1145098]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (10)
As of 2024-04-16 09:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found