Hi....

When my script does calculations, I have very small floating numbers... One example is I have '0.00008' but PHP keeps this variable as '8E-05', nothing wrong there.. Just when I store it in a database column which is defined as: decimal(10,6), it stores '8.000000'....

I guess it handles '8E-05' as a string when it stores it, thus clipping it into '8', how do I make it handle it as a float number???

Thanks

    The thing is, 8E-05 is scientific notation (a.k.a. standard form) for 8 * 10-5 and is used for floating point.

    If you arent doing any calculations from the database, only with PHP, maybe you should use a different datatype (VARCHAR perhaps?) for storage.

    I suspect there are simple methods (aside from treating it as a string then converting manually) for this, but a change of datatype for storage shouldnt be too much of a hassle.

      Yes, I do know what 8E-05 means... Just that it gets treated wrong...

      So you mean, if I have a MySQL datatype of varchar, when I get it from the database it will work as intended when trying to do calculations with it?

      Thanks

        Ack, don't use a mysql datatype.

        Instead, check this value in your php.ini:
        precision = 12
        for floating point precision. Also, you may want to use the bcmath fucntions to do your calculations instead of native php operators.

          How can I change php.ini when I have my site on a hosting server... ie, can't do anything on the server but upload files.... Can I somehow set that within the scripts??

          What are bcmatch functions??

          Thanks

            I have tried setting precision to 14 and 15.... but still the same problem... It stores 8.000000 in the database when I want 0.00008

              I'm out of ideas.

              Here's some things to double check though:
              1. try storing a float in your table using sql from the command line.
              2. try retreiving your data using sql from the command line
              3. double check your field really is a float (dumb suggestion, I know, but that's all I've got)

              Tom

                bcmath may not may not help here.
                if implemented it uses functions that treat numbers as strings and so allows calculation with very large numbers.

                the problem you are facing is one between result and storage.
                I still suggest simply changing the datatype in storage if you dont plan to use the DB itself to do calculations.

                It isnt ideal from a theoretical standpoint, but it will indeed solve your problem immediately.

                  Write a Reply...