Difference between revisions of "C#:Convert a String to a Number"

From Progzoo
Jump to: navigation, search
Line 1: Line 1:
{{topTenTips}}  
+
{{topTenTips}} {{Simple Types}}
 
<question xmlns:htm="http://www.w3.org/1999/xhtml" lang="cs" className="Demo"><prog>
 
<question xmlns:htm="http://www.w3.org/1999/xhtml" lang="cs" className="Demo"><prog>
 
using System;
 
using System;
Line 28: Line 28:
 
to add 1, giving 1235.
 
to add 1, giving 1235.
 
</question>
 
</question>
 +
<math>Insert formula here</math>

Revision as of 18:41, 29 March 2009

The static method int.Parse will an integer from a string

There is a danger that the string does not represent a valid number in which case an exception will be raised.

The class Convert may also be used for parsing strings.

Other Parse methods are available with the other base types: float.Parse long.Parse double.Parse bool.Parse

Other Convert methods include Convert.ToInt32 Convert.ToInt64 Convert.ToBoolean Convert.ToSingle Convert.ToDouble

The value "1234" is a string, you need to treat it as a number - to add 1, giving 1235.

[Font] [Default] [Show] [Resize] [History] [Profile]

<math>Insert formula here</math>