Try to parse string with Decimal Point to float in CSharp

Description

The following code shows how to try to parse string with Decimal Point to float.

Example


using System;//from  www.j a v  a 2s  .com
using System.Globalization;

public class Example
{
    public static void Main()
    {

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

        value = "1345,978";
        style = System.Globalization.NumberStyles.AllowDecimalPoint;
        culture = System.Globalization.CultureInfo.CreateSpecificCulture("fr-FR");
        if (Single.TryParse(value, style, culture, out number))
           Console.WriteLine("Converted '{0}' to {1}.", value, number);
        else
           Console.WriteLine("Unable to convert '{0}'.", value);
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var