455,204 Members | 1,465 Online Need help? Post your question and get tips & solutions from a community of 455,204 IT Pros & Developers. It's quick & easy.

# convert string to double and count characters

 P: n/a I need to convert from a string a double that is followed by a scaling character (k means *1e3, M=*1e6, etc) then apply the scaling character. Example: "-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; } ... } Feb 6 '07 #1
6 Replies

 P: n/a One way you could do this is brute force string work using IndexOfAny and then dounble.TryParse. 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. Feb 6 '07 #2

 P: n/a Thanks for your reply. 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"

 P: n/a If you are confident that your numbers will have the space delimiters separating the number strings, then you can use string.Split to get an array of the values, iterate through the values, testing the last character for 'k' or M. The double.Parse should handle the 1.03e6 just fine (maybe using the other overload that accepts a NumberStyles parameter is needed, but one of them should handle the exponential notation). 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. Feb 6 '07 #4

 P: n/a Thanks for you help ClayB + sorry for the late reply. 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"

 P: n/a Jay wrote: Thanks for your reply. 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. 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 Feb 8 '07 #6 