PC Review


Reply
Thread Tools Rate Thread

Casting a string to float

 
 
Thorsten
Guest
Posts: n/a
 
      23rd Dec 2006
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


 
Reply With Quote
 
 
 
 
Dave Sexton
Guest
Posts: n/a
 
      23rd Dec 2006
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
Dave Sexton
Guest
Posts: n/a
 
      23rd Dec 2006
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.com> wrote in message
news:%238$(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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
>>

>
>



 
Reply With Quote
 
JR
Guest
Posts: n/a
 
      23rd Dec 2006
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.com> wrote in message
news:(E-Mail Removed)...
> 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.com> wrote in message
> news:%238$(E-Mail Removed)...
>> 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" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> 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
>>>

>>
>>

>
>



 
Reply With Quote
 
TheSteph
Guest
Posts: n/a
 
      26th Dec 2006
Replace the dot with a coma, Then try convert.


"Thorsten" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Ben Voigt
Guest
Posts: n/a
 
      26th Dec 2006

"TheSteph" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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
>>
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Casting from a double to float -- please help! almurph@altavista.com Microsoft C# .NET 1 5th Mar 2009 01:56 PM
Rounding error casting from float to int 6tc1@qlink.queensu.ca Microsoft C# .NET 12 16th Mar 2006 04:51 PM
Casting Object to Float error Steve Wasser Microsoft VC .NET 1 11th Mar 2004 12:51 AM
Casting Obj to Float Steve Wasser Microsoft C# .NET 1 4th Mar 2004 12:19 AM
casting decimal to float to double error Quinn Kirsch Microsoft C# .NET 8 28th Oct 2003 10:05 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:37 AM.