472,333 Members | 2,423 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,333 software developers and data experts.

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 15333
Dormilich
8,658 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,658 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,658 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

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

Similar topics

2
by: Woodster | last post by:
I am using the std::setprecision function to format variables of type double in a string however I am unsure how to stop this appearing in ...
1
by: Nick | last post by:
Well, the project I am working on has now come to a screeching halt! I have been developing a program that heavily utilizes ADO.NET record sets. To...
7
by: Dustan | last post by:
How can I get a number into scientific notation? I have a preference for the format '1 E 50' (as an example), but if it's well known, it works.
9
by: Joe Attardi | last post by:
Hi all, Math is not my strongest area so forgive me if I use some of the wrong terminology. It seems that scientific notation is immune to...
2
by: rSmoke | last post by:
I have a DataSet that contains a table with about 6 columns of high accuracy decimal values. When I try to write out the DataSet using the...
7
by: grinder | last post by:
I have a program that fits a line to data points. The question is: can 'C' understand scientific notation (as below) , and if not what can i do to...
2
by: Robert Ludig | last post by:
When I format a number to a string in scientific notation: string.Format("{0:E}",20000); I get the following string (culture de-DE): ...
1
by: dmitri | last post by:
I am importing a bunch of data which is all in varchar format which I cast into the relevant type on transfer to the production table. One of these...
5
by: ChopperDave | last post by:
Hi, guys I am a noob at programming full stop and am having to take a unit of it at uni. I currently have an easy part to this assignment and a...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.