Reputation: 1
I have a problem with how to convert a string to a float. Here is my code on C#, please try to help me.
string Valeur = "16.2 dB";
Console.WriteLine(Valeur);
float seuil = 6;
string Valeur_optimisé = Valeur.Substring(0, Valeur.Length - 3);//Pour supprimer ( dB)
Console.WriteLine(Valeur_optimisé);
float var1 = (Convert.ToSingle(Valeur_optimisé));//J'ai une exception sur cette ligne
//Console.WriteLine(var1);
if (var1 < seuil)
{
Console.WriteLine("ERROR");
}
else
{
Console.WriteLine("OK");
}
Console.ReadKey();
Upvotes: 0
Views: 76
Reputation: 1794
You can also try:
float.Parse(Valeur_optimisé, CultureInfo.InvariantCulture.NumberFormat);
Upvotes: 0
Reputation: 419
You can use something like this using TryParse
float var1;
Single.TryParse(Valeur_optimisé, out var1);
Upvotes: 3