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 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
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
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
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
"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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
by: ghostdog |
last post by:
hi,
i got this opengl/c++ code:
<code>
void render(CMesh *mesh){
...
float *pVertices;
int *pIndices;
|
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...
|
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 }
|
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...
|
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...
|
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...
|
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...
|
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"...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
|
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...
| |