Converting String to Float

  • Thread starter Thread starter vivekaseeja
  • Start date Start date
V

vivekaseeja

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
 
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
 
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
 
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.
 
Jeff said:
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..
 
vivekian said:
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
 
Back
Top