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

Converting String to Float

P: n/a
Hi ,

Trying to convert a string value to a float value after reading the
value from an XML file , but not sure what function to use. The
following only works for integers

Int32.Parse (readXml.Value) ;

Any suggestions for an alternate function ..

thanks in advance,
vivekian

May 4 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
When parsing XML, I've always used the XmlConvert class to do all
conversions. In there, you can convert to double using the ToDouble
member. From Double to Float is a no-brainer.

Hope that helps,
Jeff

vi*********@gmail.com wrote:
Hi ,

Trying to convert a string value to a float value after reading the
value from an XML file , but not sure what function to use. The
following only works for integers

Int32.Parse (readXml.Value) ;

Any suggestions for an alternate function ..

thanks in advance,
vivekian

May 4 '06 #2

P: n/a
Learn something new every day...there is also a converstion directly to
single which will give you a single precision float.

Use the ToSingle member in the XmlConvert class.

Jeff

vi*********@gmail.com wrote:
Hi ,

Trying to convert a string value to a float value after reading the
value from an XML file , but not sure what function to use. The
following only works for integers

Int32.Parse (readXml.Value) ;

Any suggestions for an alternate function ..

thanks in advance,
vivekian

May 4 '06 #3

P: n/a
It depends on whether the string is culture-sensitive or not. If the value
in the XML file conforms to the XML Schema float type then try using
XmlConvert.ToSingle. If the string is culture-sensitive then consider using
either Convert.ToSingle or float.Parse (float is an alias for Single in C#).
Note that the three previous methods can all throw exceptions, so if you're
performing lots of conversions and you don't think you can trust the format
of the XML value, you'll need to think about how you'll handle the
exceptions. There is also a performance penalty when lots of exceptions are
thrown. An alternative is to use float.TryParse instead, which doesn't throw
exceptions if the parsing fails and so doesn't suffer any extra performance
penalty.

--
Kevin Westhead

<vi*********@gmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
Trying to convert a string value to a float value after reading the
value from an XML file , but not sure what function to use. The
following only works for integers

Int32.Parse (readXml.Value) ;

May 4 '06 #4

P: n/a

Jeff wrote:
Learn something new every day...there is also a converstion directly to
single which will give you a single precision float.

Use the ToSingle member in the XmlConvert class.

Jeff


yes .. could you show this with the help of a small example :p . Cant
find any useful examples on the net.

thanks..

May 4 '06 #5

P: n/a

vivekian wrote:
Jeff wrote:
Learn something new every day...there is also a converstion directly to
single which will give you a single precision float.

Use the ToSingle member in the XmlConvert class.

Jeff


yes .. could you show this with the help of a small example :p . Cant
find any useful examples on the net.

thanks..


Actually , got it working .. thanks a ton :) XmlConvert did the trick
:)
vivekian

May 4 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.