By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 787 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

Casting a string to float

P: n/a
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
Dec 23 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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" <no****@nospam.comwrote in message
news:OA*************@TK2MSFTNGP06.phx.gbl...
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

Dec 23 '06 #2

P: n/a
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" <dave@jwa[remove.this]online.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
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" <no****@nospam.comwrote in message
news:OA*************@TK2MSFTNGP06.phx.gbl...
>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


Dec 23 '06 #3

P: n/a
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" <dave@jwa[remove.this]online.comwrote in message
news:ek*************@TK2MSFTNGP06.phx.gbl...
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" <dave@jwa[remove.this]online.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
>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" <no****@nospam.comwrote in message
news:OA*************@TK2MSFTNGP06.phx.gbl...
>>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



Dec 23 '06 #4

P: n/a
Replace the dot with a coma, Then try convert.
"Thorsten" <no****@nospam.comwrote in message
news:OA*************@TK2MSFTNGP06.phx.gbl...
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


Dec 26 '06 #5

P: n/a

"TheSteph" <Th******@NoSpam.comwrote in message
news:O7**************@TK2MSFTNGP04.phx.gbl...
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.
>

"Thorsten" <no****@nospam.comwrote in message
news:OA*************@TK2MSFTNGP06.phx.gbl...
>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



Dec 26 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.