"-1.345k #comment"
I know roughly how to do this in C (using the %n in the sscanf format specifier to find the number
of characters converted) , but how do I do it in C#? My problem is how to count the number of
characters of the double (chpos in the C example) so that the scaling character can be easily found.
/* C-code example */
double doub;
int n, nchar, chpos=0
char str[] = "-1.345k #comment"
n=sscanf"(&str[chpos], "%lf%n", &doub, &nchar);
if(n==0){
... code to deal with invalid double
}else
chpos += nchar;
if(str[chpos]='k'){
doub *= 1e6;
}
...
}
string str = "-1.35k #comment";
int i = str.IndexOfAny(new char[]{'k', 'M', '#'});
double d = double.NaN;
if(i == -1 || str[i] == '#')
{
//error case
}
else if(double.TryParse(str.Substring(0, i), out d))
{
switch(str[i])
{
case 'k':
d *= 1000;
break;
case 'M':
d *= 1000000;
break;
default:
break;
}
}
else
{
//second error case
}
//d has the value
======================
Clay Burch
Syncfusion, Inc.
Sorry, but my example was a little simplified to help to illustrate my problem. A real example is
string str = "load=-1.35k source=2.3e6 sink=34,27M #comment";
If I use your technique, it misses the absence of a scaling value (which is allowed, as shown for
source=2.3e6).
If C# doesn't have a way to count the number of characters used by the double, then I guess I'll
have to write a method that does the counting, taking into account all the possible ways that a
valid double can be written.
"ClayB" <cl...@syncfusion.com> wrote in message
news:1170756494.5...@a34g2000cwb.googlegroups.com...
string str = "load=-1.35k source=2.3e6 sink=34,27M #comment";
Console.WriteLine(str);
string[] s = str.Split(new char[] { '=', ' '});
for(int i = 1; i < s.GetLength(0); i += 2)
{
double mult = 1;
if (s[i].EndsWith("k"))
{
mult = 1000;
s[i] = s[i].Substring(0, s[i].Length - 1);
}
else if (s[i].EndsWith("M"))
{
mult = 1000000;
s[i] = s[i].Substring(0, s[i].Length - 1);
}
double d;
if(double.TryParse(s[i], System.Globalization.NumberStyles.Any,
null, out d))
{
d = mult * d;
Console.WriteLine(d);
}
}
//output
load=-1.35k source=2.3e6 sink=34,27M #comment
-1350
2300000
3427000000
==============
Clay Burch
Syncfusion, Inc.
It works fine, although I'm a little worried about coping with errors. For instance, if the line
starts with "load -1.35k" rather than "load=-1.35k", the value is read, even though the syntax is
incorrect.
"ClayB" <cl...@syncfusion.com> wrote in message
news:1170787613.5...@v33g2000cwv.googlegroups.com...
You can use a regular expression to make sure that the format is
correct, and extract the interresting information.
You can use a pattern like this:
"^load=([\-\d\.])([kM]) source=([\-\d\.](?:e\d+)?)
sink=([\d,])([kM])\s*(?:#.*)$"
This will match anything outside the parantheses literarly, except the
comment, that is optional. Inside the parantheses it will match
according to the patterns, allowing the characters used in numbers.
What's in every paranthesis pair is returned as groups.
You can make the matches stricter if you like, for example only allowing
the minus sign as first character and only allowing a single decimal
separator. You can also make some matches more relaxed, like allowing
any number of spaces between the values.
When you match the input string using the pattern, you will only get a
match if the input is correct according to the pattern. If you don't get
a match, you know that there is something wrong with the line.
--
Göran Andersson
_____
http://www.guffa.com
Jay
"Göran Andersson" <gu...@guffa.com> wrote in message news:%23No$al5SHH...@TK2MSFTNGP03.phx.gbl...