TryParse
(1)
IndexOf
(1)
Walker
(1)
Math
(1)
Substring
(1)
Parse
(1)
Power
(1)
Pow
(1)

Best way to convert string to float/double/decimal?

Asked By Chris Walker
11-May-07 03:22 AM
What has everyone found is the most efficient way to convert strings to
numerical types in the Micro Framework?

For instance, in the full .NET framework:
String magicNumberString = "4.62";
float magicNumber = float.Parse(magicNumberString);

Since there's no Parse option on numerical types in Micro Framework, what is
everyone else using?

Chris
---
Chris Walker
ControlThink LC
http://www.controlthink.com

Ah...

Asked By James Webster [MSFT]
15-May-07 12:50 PM
Ah... I knew that a customer would bring this up sooner or later as I
recently submitted a bug request to add this functionality.  I had a similar
requirement for a demo I was working on for the Digi device.  Its funny how
often you have to go string -> value when programming network apps.

In the mean time I wrote the following function to work around the missing
functionality.  Please note there is little to no error handling and does
not work with formated number strings (ie commas/globalized strings) The
embedded link is where I found the algorithm for a generic string to number
funtion:

public static bool TryParseFloat(string s, out float result)
{
// Algorithm from
http://www.geocities.com/oosterwal/computer/numericbase.html
bool success = true;
string numberBase = "0123456789";
bool match = false;
int decimalPos = s.IndexOf('.');
result = 0;

// find decimal
if (decimalPos != -1)
{
// check to see if . is at end of string
if (decimalPos == s.Length - 1)
s = s.Substring(0, decimalPos);
else
s = s.Substring(0, decimalPos) + s.Substring(decimalPos + 1);
}

try
{
for (int i = 0; i < s.Length; i++)
{
match = false;
for (int j = 0; j < numberBase.Length; j++)
{
if (s[i] == numberBase[j])
{
result += (long)(((j) * System.Math.Pow(10, (s.Length -
(i + 1)))) + 0.5);
match = true;
break;
}
}
if (!match)
success = false;
}

if (decimalPos > -1)
result = (float)(result / (System.Math.Pow(10, s.Length -
decimalPos)));
}
catch
{
success = false;
}

return success;
}


--
---
James Webster
Microsoft .Net Micro Framework
Test Manager

This posting is provided "AS IS" with no warranties, and confers no rights.
Please do not send email directly to this alias. This alias is for newsgroup
purposes only.

Best way to convert string to float/double/decimal?

Asked By Ben Voigt
17-May-07 02:34 PM
Oh no!


....


You are so kidding me!


no, no, no, no!


The words "performance", "low power", and "small footprint" really don't
mean anything to you, do they?


This is a joke, right?


IMHO, you've just lost any credibility with experienced embedded developers.

Best way to convert string to float/double/decimal?

Asked By Ben Voigt
17-May-07 02:36 PM
You might take a look at the System.Number.ParseNumber method in the desktop
framework (using Roeder's .NET Reflector).
Best way to convert string to float/double/decimal?
Asked By Jens Kühner
17-May-07 05:33 PM
See my blog at http://bloggingabout.net/blogs/jens, there is a number parser
class.
It is based on the internal NumberParser class from the DotGNU Portable.NET
project and I ported and simplified it to the .NET Micro Framework. It can
parse integers, hex-numbers and floating point numbers and also handles
exponents and thousands group separators. There is still room for
optimizations. Please let me know if you did any optimizations.

I wrote this class for my .NET Micro Framework book, which I am currently
writing. Stay tuned for more informations about it.



regards

Jens Kühner
Ben, thanks for your feedback, it is appreciated.
Asked By James Webster [MSFT]
18-May-07 02:37 PM
Ben, thanks for your feedback, it is appreciated.  That said, please keep in
mind this forum is to help each other.  Your 2nd post was much more in than
that spirit then the 1st.  My post was something I threw together quickly as
I had seen no response to Chris's post for 4 days.

Here's a rework of the function using your suggestions (single pass, no
Math.Power).
public static bool TryParseFloat(string s, out float result)
{
bool success = true;
int decimalDiv = 0;
result = 0;
try
{
for (int i = 0; i < s.Length; i++)
{
if (s[i] == '.' && decimalDiv == 0)
decimalDiv = 1;
else if (s[i] < '0' || s[i] > '9')
success = false;
else
{
result = result * 10;
decimalDiv = decimalDiv * 10;
result += (int)(s[i] - '0');
}
}
result = (float) result / decimalDiv;
}
catch
{
success = false;
}
return success;
}


--
---
James Webster
Microsoft .Net Micro Framework
Test Manager

This posting is provided "AS IS" with no warranties, and confers no rights.
Please do not send email directly to this alias. This alias is for newsgroup
purposes only.
Best way to convert string to float/double/decimal?
Asked By Ben Voigt
22-May-07 09:45 AM
And your new function is very much superior in all respects.  Only problem I
can see is easily fixed.  I'm +1 for adding this to the framework, along
with some unit tests.


should maybe be float?


I assume the C# compiler will do subexpression analysis and get the same
result anyway, but I think the code would be a little cleaner to cache s[i]:

char digit = s[i];


if (decimalDiv != 0)
I can't seem to get more than "0x" from that attachment on your blog.
Asked By Michel Verhagen (eMVP)
12-Jun-07 07:33 PM
I can't seem to get more than "0x" from that attachment on your blog. Is
is possible to post the code here so we can all take a look?

Thanks,

Michel Verhagen, eMVP
EmbeddedFusion
www.EmbeddedFusion.com
mverhagen at embeddedfusion dot com


--
Best way to convert string to float/double/decimal?
Asked By Jens Kuehner
30-Jul-07 04:28 PM
There is an update available on my blog at
http://bloggingabout.net/blogs/jens/archive/2007/07/30/update-to-number-parsing-with-the-net-micro-framework.aspx.
It has now TryParse methods and is a little more compact.

Jens
Post Question To EggHeadCafe