Welcome to Dream.In.Code
Become a C# Expert!

Join 150,399 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 967 people online right now. Registration is fast and FREE... Join Now!




Scientific notation question

 
Reply to this topicStart new topic

Scientific notation question, silly question, perhaps

killnine
10 Mar, 2008 - 01:39 PM
Post #1

D.I.C Head
**

Joined: 12 Feb, 2007
Posts: 114



Thanked: 5 times
My Contributions
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)

CODE

                    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?
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Scientific Notation Question
10 Mar, 2008 - 02:17 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 314 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
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()

csharp

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. smile.gif

This post has been edited by Martyr2: 10 Mar, 2008 - 02:17 PM
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Scientific Notation Question
10 Mar, 2008 - 08:15 PM
Post #3

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,327



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

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

CODE

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

User is offlineProfile CardPM
+Quote Post

killnine
RE: Scientific Notation Question
11 Mar, 2008 - 05:42 AM
Post #4

D.I.C Head
**

Joined: 12 Feb, 2007
Posts: 114



Thanked: 5 times
My Contributions
QUOTE(jayman9 @ 10 Mar, 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

CODE

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




WellI have tried two ways and neither seem to work.

CODE

                    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.gif 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:

CODE

                    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 mad.gif

This post has been edited by killnine: 11 Mar, 2008 - 05:43 AM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Scientific Notation Question
15 Mar, 2008 - 08:09 PM
Post #5

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 314 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
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) ...

csharp

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!" decap.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 06:54PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month