469,323 Members | 1,596 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

Convert from Scientific Notation

code green
1,726 Expert 1GB
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?
Dec 8 '09 #1
8 14486
Dormilich
8,651 Expert Mod 8TB
try type casting.

e.g.
Expand|Select|Wrap|Line Numbers
  1. $bar = (float) $foo;
Dec 8 '09 #2
code green
1,726 Expert 1GB
Never thought of cast. Thanks.
But I don't want to cast a float to an int or vice-versa.
So.....
Expand|Select|Wrap|Line Numbers
  1. if(is_float($value)
  2.     $value = (float)$value;
  3. if(is_int($value)
  4.     $value = (int)$value;
And according to the manual these functions can handle scientific notation
Expand|Select|Wrap|Line Numbers
  1. if(is_float(27.25)) {
  2.  echo "is float\n";
  3. }else {
  4.  echo "is not float\n";
  5. }
  6. var_dump(is_float('abc'));
  7. var_dump(is_float(23));
  8. var_dump(is_float(23.5));
  9. var_dump(is_float(1e7));  //Scientific Notation  <<<<<<<<<
  10. var_dump(is_float(true));
  11.  
I'll try that. Thanks again
Dec 8 '09 #3
code green
1,726 Expert 1GB
Removed this post. Still testing
Dec 8 '09 #4
hsriat
1,654 Expert 1GB
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.
Dec 8 '09 #5
Dormilich
8,651 Expert Mod 8TB
side note:
But I don't want to cast a float to an int or vice-versa.
but maybe a string to a float.
Dec 8 '09 #6
code green
1,726 Expert 1GB
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
Expand|Select|Wrap|Line Numbers
  1. if(is_float($value) 
  2.     $value = (float)$value; 
  3. if(is_int($value) 
  4.     $value = (int)$value; 
  5.  
The value 1.93E+11 returns false for is_float and is_int.
I am assuming they are seen as strings.
I can use
Expand|Select|Wrap|Line Numbers
  1. if(is_numeric($value))
which will return true for numeric strings but doesn't identify whether float or int
Dec 8 '09 #7
Dormilich
8,651 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. if(is_float($value)) 
  2.     $value = (float)$value; 
  3. if(is_int($value)) 
  4.     $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.
Expand|Select|Wrap|Line Numbers
  1. // foreach
  2. $num = (float) $num;
Dec 8 '09 #8
code green
1,726 Expert 1GB
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
Dec 8 '09 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Woodster | last post: by
7 posts views Thread by Dustan | last post: by
9 posts views Thread by Joe Attardi | last post: by
7 posts views Thread by grinder | last post: by
5 posts views Thread by ChopperDave | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.