Casting a string to float

T

Thorsten

Hi everyone,

I am rather new to C# and have a problem that will probably seem trivial to
most of you... but I hope you can still help me nevertheless..

Via the comport, I read the result of a digital scale... the result is sent
as a string like "+0000.23kg", representing the weight in kilograms.

In order to work with the returned value, I need to use it as a float or
decimal or double... I tried casting it via Convert, via float.Parse etc...
but I always get either a wrong value (eg. "0000.24" results in "24" instead
of "0.24") or an exeption.

Does anyone have an Idea how I can get this as a float?

Thanks!

Thorsten
 
D

Dave Sexton

Hi Thorsten,

You could try using a regular expression:

Match match = Regex.Match(input, "[\+\-](?<N>\d*\(\.\d+)?)(?<S>\w+)");

if (!match.Success)
throw new FormatException("Invalid input data.");

float number = float.Parse(match.Groups["N"].Value);
string scale = match.Groups["S"].Value;

(I didn't test this code so it might need some mods.)

Regular Expression Language Elements
http://msdn2.microsoft.com/en-us/library/az24scfc.aspx
 
D

Dave Sexton

Hi Thorsten,

I just realized that I didn't include the sign in the N group and added an
extra \. Correction:
+0000.23kg

(?<N>[\+\-]?\d*(\.\d+)?)(?<S>\w+)

--
Dave Sexton

Dave Sexton said:
Hi Thorsten,

You could try using a regular expression:

Match match = Regex.Match(input, "[\+\-](?<N>\d*\(\.\d+)?)(?<S>\w+)");

if (!match.Success)
throw new FormatException("Invalid input data.");

float number = float.Parse(match.Groups["N"].Value);
string scale = match.Groups["S"].Value;

(I didn't test this code so it might need some mods.)

Regular Expression Language Elements
http://msdn2.microsoft.com/en-us/library/az24scfc.aspx

--
Dave Sexton

Thorsten said:
Hi everyone,

I am rather new to C# and have a problem that will probably seem trivial
to most of you... but I hope you can still help me nevertheless..

Via the comport, I read the result of a digital scale... the result is
sent as a string like "+0000.23kg", representing the weight in kilograms.

In order to work with the returned value, I need to use it as a float or
decimal or double... I tried casting it via Convert, via float.Parse
etc... but I always get either a wrong value (eg. "0000.24" results in
"24" instead of "0.24") or an exeption.

Does anyone have an Idea how I can get this as a float?

Thanks!

Thorsten
 
J

JR

1. I think the word casting is inappropriate, this is a conversion of
external data to an internal format rather than conversion of data from one
internal format to the other.

2. Take a look at Double.TryParse.

3. If it is always kg you can remove it with substring.

JR

Dave Sexton said:
Hi Thorsten,

I just realized that I didn't include the sign in the N group and added an
extra \. Correction:
+0000.23kg

(?<N>[\+\-]?\d*(\.\d+)?)(?<S>\w+)

--
Dave Sexton

Dave Sexton said:
Hi Thorsten,

You could try using a regular expression:

Match match = Regex.Match(input, "[\+\-](?<N>\d*\(\.\d+)?)(?<S>\w+)");

if (!match.Success)
throw new FormatException("Invalid input data.");

float number = float.Parse(match.Groups["N"].Value);
string scale = match.Groups["S"].Value;

(I didn't test this code so it might need some mods.)

Regular Expression Language Elements
http://msdn2.microsoft.com/en-us/library/az24scfc.aspx

--
Dave Sexton

Thorsten said:
Hi everyone,

I am rather new to C# and have a problem that will probably seem trivial
to most of you... but I hope you can still help me nevertheless..

Via the comport, I read the result of a digital scale... the result is
sent as a string like "+0000.23kg", representing the weight in
kilograms.

In order to work with the returned value, I need to use it as a float or
decimal or double... I tried casting it via Convert, via float.Parse
etc... but I always get either a wrong value (eg. "0000.24" results in
"24" instead of "0.24") or an exeption.

Does anyone have an Idea how I can get this as a float?

Thanks!

Thorsten
 
B

Ben Voigt

TheSteph said:
Replace the dot with a coma, Then try convert.

That's a good point, you could have locale issues. Use the overload of
double.TryParse that accepts an IFormatProvider and use the invariant
culture.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top