Convert from Scientific Notation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    Convert from Scientific Notation

    I am being sent a CSV file with ID numbers somehow converted to scientific format ie 1.93E+11 and 3.70E+9.
    In theory the same thing could happen to telephone numbers or even currency.
    I am trying to think of a generic solution to convert such numbers back to integers or floats before inserting into MySql
    number_format() and sprintf() come to mind but these are more for formatting print output.
    What is the best way to convert these numbers into non-scientific format?
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    try type casting.

    e.g.
    Code:
    $bar = (float) $foo;

    Comment

    • code green
      Recognized Expert Top Contributor
      • Mar 2007
      • 1726

      #3
      Never thought of cast. Thanks.
      But I don't want to cast a float to an int or vice-versa.
      So.....
      Code:
      if(is_float($value)
          $value = (float)$value;
      if(is_int($value)
          $value = (int)$value;
      And according to the manual these functions can handle scientific notation
      Code:
      if(is_float(27.25)) {
       echo "is float\n";
      }else {
       echo "is not float\n";
      }
      var_dump(is_float('abc'));
      var_dump(is_float(23));
      var_dump(is_float(23.5));
      var_dump(is_float(1e7));  //Scientific Notation  <<<<<<<<<
      var_dump(is_float(true));
      I'll try that. Thanks again

      Comment

      • code green
        Recognized Expert Top Contributor
        • Mar 2007
        • 1726

        #4
        Removed this post. Still testing
        Last edited by code green; Dec 8 '09, 01:57 PM. Reason: Wrong answer submitted

        Comment

        • hsriat
          Recognized Expert Top Contributor
          • Jan 2008
          • 1653

          #5
          Open your CSV files with TextPad/Notepad or something similar. If the file still shows the numbers in the form of 9.1E+10, you have already lost the information with MS Excel's stupid auto conversion of number format to scientific format.

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            side note:
            But I don't want to cast a float to an int or vice-versa.
            but maybe a string to a float.

            Comment

            • code green
              Recognized Expert Top Contributor
              • Mar 2007
              • 1726

              #7
              You're right hsriat that is what is happening, but I have no control over their creation.
              They are created by ebay, go via a third party, from where they are FTP down.
              Unfortunately this isn't working
              Code:
              if(is_float($value) 
                  $value = (float)$value; 
              if(is_int($value) 
                  $value = (int)$value;
              The value 1.93E+11 returns false for is_float and is_int.
              I am assuming they are seen as strings.
              I can use
              Code:
              if(is_numeric($value))
              which will return true for numeric strings but doesn't identify whether float or int

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                Code:
                if(is_float($value)) 
                    $value = (float)$value; 
                if(is_int($value)) 
                    $value = (int)$value;
                pointless, because they already have the desired data type.

                why don’t you cast all number strings to float? then you’d have a single data type for all.
                Code:
                // foreach
                $num = (float) $num;

                Comment

                • code green
                  Recognized Expert Top Contributor
                  • Mar 2007
                  • 1726

                  #9
                  pointless, because they already have the desired data type
                  Of course!! Bang head, bang head.
                  Cast all to float? I am a little nervous of that idea.
                  Floats can be un-predictable, some basic computer science about how their values are actually stored.
                  But only usually happens when performing calculations so could get away with it

                  Comment

                  Working...