0

I'm reading a txt file containing + & - decimals which may contain exponential notations:

6.636666E-08
0
-5.5
2.2

I need to order the decimals then write them back to a different txt file.

I'm using decimal.Parse() to add the decimals into a dictionary, then I order them. When I do that I lose the exponential notaions.

decimal.Parse() converts 6.636666E-08 to : 0.00000006636666

I need to write the original values as they were(just ordered).

How can I do that efficiently efficiently ?

How can I round the exponential notation issue ? The file might contain thousands of lines, is decimal.Parse() an efficient way of doing it?

Thank you

Walid
  • 187
  • 1
  • 3
  • 11
  • What language are you using?! Does [this](http://stackoverflow.com/questions/3879463/parse-a-number-from-exponential-notation) answer your question? – DilithiumMatrix Oct 13 '15 at 21:21
  • I'm using c#. I'm using decimal d = Decimal.Parse("6.636666E-08",System.Globalization.NumberStyles.Float); That converts 6.636666E-08 to 0.00000006636666 which is fine for my ordering but then I need to write it to the txt file in its original format i.e. 6.636666E-08 How can I convert 0.00000006636666 back to 6.636666E-08 ? Is there a way of doing all this efficiently ? Thanks – Walid Oct 13 '15 at 21:47
  • Can you post some code? One way to do it is to have a list of tuples that you sort, one is the parsed decimal, the other is a string. Sort the decimal, and then print the string to file (Assuming the notation is not predictable - if it's predictable, write a helper when you print it to format it to whatever it should be) – Prescott Oct 13 '15 at 21:48

1 Answers1

3

I need to order the decimals then write them back to a different txt file.

If you need to guarantee that the written values are exactly as they appear in the source, then keep them as strings and order them by the parsed value:

var a = {read lines to string array}
var q = a.OrderBy(s => decimal.Parse(s));

foreach(string s in q)
    // write s

Also, don't use a dictionary if need to order the values - dictionaries are unordered by design.

D Stanley
  • 149,601
  • 11
  • 178
  • 240