System.Globalization.NumberStyles.AllowDecimalPoint | System.Globalization.NumberStyles.AllowThousands; : float parse « Data Types « C# / C Sharp






System.Globalization.NumberStyles.AllowDecimalPoint | System.Globalization.NumberStyles.AllowThousands;

 
using System;
using System.Globalization;

public class Example
{
    public static void Main()
    {

        string value;
        System.Globalization.NumberStyles style;
        System.Globalization.CultureInfo culture;
        float number;

        value = "1.345,978";
        style = System.Globalization.NumberStyles.AllowDecimalPoint | 
                System.Globalization.NumberStyles.AllowThousands;
        culture = System.Globalization.CultureInfo.CreateSpecificCulture("es-ES");
        if (Single.TryParse(value, style, culture, out number))
           Console.WriteLine("Converted '{0}' to {1}.", value, number);
        else
           Console.WriteLine("Unable to convert '{0}'.", value);
        
        value = "1 345,978";
        if (Single.TryParse(value, style, culture, out number))
           Console.WriteLine("Converted '{0}' to {1}.", value, number);
        else
           Console.WriteLine("Unable to convert '{0}'.", 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 to float
9.Converts string in a specified style to float
10.NumberStyles.AllowParentheses | NumberStyles.AllowTrailingSign | NumberStyles.Float | NumberStyles.AllowThousands
11.Converts string in a specified style and culture-specific format to float
12.Read value from console and convert it to float
13.Parse currency value using en-GB culture.
14.Convert string to float with NumberStyles.AllowDecimalPoint
15.Parse a negative integer value.