Converts string to float : float parse « Data Types « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » Data Types » float parse 




Converts string to float
 


using System;

public class Example
{
   public static void Main()
   {
      string[] values = "(100)""-123,456,789""123.45e+6"
                          "+500""5e2""3.1""6.""-.123"
                          "-Infinity""-1E-16", Double.MaxValue.ToString()
                          Single.MinValue.ToString(), String.Empty };
      foreach (string value in values)
      {
         try {   
            float number = Single.Parse(value);
            Console.WriteLine("{0} -> {1}", value, number);
         }
         catch (FormatException) {
            Console.WriteLine("'{0}' is not in a valid format.", value);
         }
         catch (OverflowException) {
            Console.WriteLine("{0} is outside the range of a Single.", value);
         }
      }                                  
   }
}

   
  














Related examples in the same category
1.Parse a string in exponential notation with only the AllowExponent flag
2.Parse a string in exponential notation with the AllowExponent and Number flags.
3.Parse a currency value with leading and trailing white space, and white space after the U.S. currency symbol.
4.Parse negative value with thousands separator and decimal.
5.Parse a floating-point value with a thousands separator.
6.Parse a floating-point value with a currency symbol and a thousands separator.
7.Parse value in exponential notation.
8.Converts string in a specified style to float
9.NumberStyles.AllowParentheses | NumberStyles.AllowTrailingSign | NumberStyles.Float | NumberStyles.AllowThousands
10.Converts string in a specified style and culture-specific format to float
11.Read value from console and convert it to float
12.Parse currency value using en-GB culture.
13.Convert string to float with NumberStyles.AllowDecimalPoint
14.System.Globalization.NumberStyles.AllowDecimalPoint | System.Globalization.NumberStyles.AllowThousands;
15.Parse a negative integer value.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.