Scientific notation question

silly question, perhaps

Page 1 of 1

4 Replies - 21909 Views - Last Post: 15 March 2008 - 09:09 PM Rate Topic: -----

#1 killnine  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 161
  • Joined: 12-February 07

Scientific notation question

Posted 10 March 2008 - 02:39 PM

Silly question,

I have a program that populates a grid with data from a device. The grid has a column of doubles (I am using the 3rd party grid, SourceGrid, an excellent C# project) and most of them are just dandy.

However, I have one really small number (.000086669) that gets converted to scientific notation (8.6669E-5) in the grid.

My application also has a feature to pull the values FROM the grid and write them to file. However, they have to be in double format still, and pulling out the values means I will get that dastardly 8.6669E-5!

I have tried the following to no avail (meaning, invalid format exception)

					if (varDbRow[2].ToString().Contains("E")) // Check for scientific notation in the particular Cell
					{
//If it exists, parse the value to remove the scientific notation
						varDbRow[2] =
							Double.Parse(varDbRow[2].ToString(), System.Globalization.NumberStyles.AllowExponent);
					}



However, this doesn't work because the significand (8.6669) contains a decimal. D'oh


So I have thought about trying something using the string.Split() method, and adding zeros to the beginning (with decimal) or end (without decimal) of the significand depending on whether the mantissa (-5, in this case) is positive or negative. But that seems like a LOT of work for something that seems so simple.

Thoughts?

Is This A Good Question/Topic? 0
  • +

Replies To: Scientific notation question

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4422
  • View blog
  • Posts: 12,289
  • Joined: 18-April 07

Re: Scientific notation question

Posted 10 March 2008 - 03:17 PM

Well you got to remember that the scientific notation there is still a double. It is just be represented in such a way to shorten it up. So your double variable is fine, you just need to format it for writing it to the file. You can do that with String.Format()

String.Format("{0:.###########}",doublevariablehere)



This would pad it out with zeroes into a string which can then be written to file as a double value like .000086669. When you read it back in from the file and dump it to double, it again will go back to scientific notation.

This notation is just for formatting purposes here. 8.6669e-05 = .000086669. They are the same value, just displayed differently.

Hope that helps. :)

This post has been edited by Martyr2: 10 March 2008 - 03:17 PM

Was This Post Helpful? 0
  • +
  • -

#3 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Scientific notation question

Posted 10 March 2008 - 09:15 PM

You could also use the ToString method to format numerics. F9 means fixed point with a precision of 9 places. Standard Numeric Format Strings

Double.Parse(varDbRow[2].ToString("F9"), System.Globalization.NumberStyles.AllowExponent);

Was This Post Helpful? 0
  • +
  • -

#4 killnine  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 161
  • Joined: 12-February 07

Re: Scientific notation question

Posted 11 March 2008 - 06:42 AM

View Postjayman9, on 10 Mar, 2008 - 09:15 PM, said:

You could also use the ToString method to format numerics. F9 means fixed point with a precision of 9 places. Standard Numeric Format Strings

Double.Parse(varDbRow[2].ToString("F9"), System.Globalization.NumberStyles.AllowExponent);



WellI have tried two ways and neither seem to work.

					if (varDbRow[2].ToString().Contains("E")) // Check for scientific notation
					{
					  double result = double.Parse(varDbRow[2]);
						varDbRow[2] = (Double.Parse(result.ToString("F9"), NumberStyles.AllowExponent)).ToString();
					}



I had to create a local double because in order to use ToString I must have a non-nullable variable. :crazy: Anyhow, I get the error:

Quote

An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

Additional information: Input string was not in a correct format.


The string that comes in (varDbRow[2]) is EXACTLY 8.6669E-05. Is the zero throwing the compiler or what? I just don't get it.

When I try this:

					if (varDbRow[2].ToString().Contains("E")) // Check for scientific notation
					{
						//double result = double.Parse(varDbRow[2]);
						//varDbRow[2] = (Double.Parse(result.ToString("F9"), NumberStyles.AllowExponent)).ToString();
						String.Format("{0:.###########}", double.Parse(varDbRow[2]));  
					}



It actually works at runtime, but when I check the final file output, it has not converted anything: it outputs 9.00E-05, and it has lost some accuracy then.

Whats the deal? This shouldn't be so damn difficult. All I need it to do is stay out of scientific notation :angry:

This post has been edited by killnine: 11 March 2008 - 06:43 AM

Was This Post Helpful? 0
  • +
  • -

#5 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4422
  • View blog
  • Posts: 12,289
  • Joined: 18-April 07

Re: Scientific notation question

Posted 15 March 2008 - 09:09 PM

But are you using the formatting as part of the write to the file? You have to use the formatting as part of your writing. Below is an example (written in a quick C# console app) ...

public void Run()
{
     // Create a stream to the test file for writing.
     StreamWriter s = new StreamWriter(@"C:\\testing.txt");

     // Write the stream using the formatting
     // Result in file is that it prints .00009
     s.WriteLine(String.Format("{0:.###########}", 9.00E-05));
     s.Close();
}



Throw that into a console app to test it out if you want. It will create a "testing.txt" file in your c:\ folder and it will contain your double in the correct decimal form.

Enjoy!

"At DIC we be decimal extending and formatting code ninjas!" :snap:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1