1

I am communicating with a measurement isntrument and it returns the data in a single string. The output is like:

string result = "-3.546714E-10A,+0.000000E+00,+5.120000E+02\n";

So what I am interested to get in this string is first and second part. First part is Current in Ampere, and second is TimeStamp (measurement time).

I am trying this code but it does not work:

temp = result.Split(',');

tipair = new Results.TIPair();
tipair.Current = float.Parse(temp[0]);
tipair.Time = float.Parse(temp[1]);

Ideally I want it to be -3.546714E-10 for current and 0 for time (time be any positive number).

UPDATE: I tried to do like below but I get very bad numbers (much bigger than they should be!)

temp = result.Split(',');

tipair = new Results.TIPair();
tipair.Current = float.Parse(temp[0].Substring(0, temp[0].Length - 1));
tipair.Time = float.Parse(temp[1]);
Dumbo
  • 13,555
  • 54
  • 184
  • 288

2 Answers2

3

You can parse current using Double.Parse() and valid Number Styles:

string result = "-3.546714E-10A,+0.000000E+00,+5.120000E+02\n";
var parts = result.Split(',');

double current = double.Parse(parts[0].Remove(parts[0].Length - 1),
                        NumberStyles.AllowExponent | NumberStyles.Number);
double time = double.Parse(parts[1],
                        NumberStyles.AllowExponent | NumberStyles.Number);

MSDN, NumberStyles Enumeration:

AllowExponent Indicates that the numeric string can be in exponential notation. The AllowExponent flag allows the parsed string to contain an exponent that begins with the "E" or "e" character and that is followed by an optional positive or negative sign and an integer. In other words, it successfully parses strings in the form nnnExx, nnnE+xx, and nnnE-xx. It does not allow a decimal separator or sign in the significand or mantissa; to allow these elements in the string to be parsed, use the AllowDecimalPoint and AllowLeadingSign flags, or use a composite style that includes these individual flags.

sll
  • 61,540
  • 22
  • 104
  • 156
  • Thanks for details. Is it really should be DOUBLE? Because later I want to draw them on a bitmap and I think I am limited to PointF which only works with float?? – Dumbo Oct 28 '11 at 09:18
  • You can change double to float and all would be parsed fine as well – sll Oct 28 '11 at 09:34
2

It doesnt know the Ampere unity in the first part...

you try to parse "-3.546714E-10A" to float.

You should first cut the "A" off with:

part[0].TrimEnd('A')

UPDATE:

check Convert scientific to float question for scientific parse

by the way: float can handle a precision of 7 characters (which you are fully using). if you get more exact values you should use double (15-16 precision)

Community
  • 1
  • 1
fixagon
  • 5,506
  • 22
  • 26
  • I tried to remove that character by substring but it gives me a biggg number. mayeb the floating points are not read correctly? – Dumbo Oct 28 '11 at 08:55
  • hmm dont know, i dont have a compiler under my hands right now... but something like that :) – fixagon Oct 28 '11 at 09:05
  • @Sean87 : TrimEnd(), there is no such method like TrimRight() – sll Oct 28 '11 at 12:16