473,320 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Casting a string to float

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
5 5248
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
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
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Cgacc20 | last post by:
I have a c struct from old code that cannot be modified and I am trying to write a wrapper C++ class around it. This class is often passed as a pointer to some c functions of a library and I...
2
by: ghostdog | last post by:
hi, i got this opengl/c++ code: <code> void render(CMesh *mesh){ ... float *pVertices; int *pIndices;
2
by: philippe sillon | last post by:
Hi, I have a problem to implemente the strategy pattern. the problem come from that a method take different arguments type (object in interface and String / Int in implemented class). When...
16
by: Enekajmer | last post by:
Hi, 1 int main() 2 { 3 float a = 17.5; 4 printf("%d\n", a); 5 printf("%d\n", *(int *)&a); 6 return 0; 7 }
12
by: 6tc1 | last post by:
Hi all, I just discovered a rounding error that occurs in C#. I'm sure this is an old issue, but it is new to me and resulted in a fair amount of time trying to track down the issue. Basically...
3
by: LongBow | last post by:
Hello all, First of all, sorry for multiple question per one thread. I have two questions. First what I think might be the easier problem. I am capturing data from an embedded device which I...
13
by: Gary Wessle | last post by:
Hi there I have a method which returns time_t and another two methods return double data types and I cann't change that since the library is provided by Big Bucks Inc. I think time_t is long but...
17
by: sophia.agnes | last post by:
Hi , I was going through peter van der linden's book Expert C programming, in this book there is a section named "How and why to cast" the author then says as follows (float) 3 - it's a...
32
by: alex.j.k2 | last post by:
Hello all, I have "PRECISION" defined in the preprocessor code and it could be int, float or double, but I do not know in the code what it is. Now if I want to assign zero to a "PRECISION"...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.