473,379 Members | 1,185 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 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 15717
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 scientific notation. For example std::stringstream...
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 generate reports, I convert the recordset to XML,...
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 rounding errors. For example: (4.98 * 100) + 5.51 ...
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 WriteXML() function the XML is written fine, but 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 make it work.. 0.000 0.3231E+02 0.0000E+00 ...
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): 2,000000E+003 How can I format scientific notation in...
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 columns contains decimals, but some of them are...
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 hard part. I am finishing up the easy part currently...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.