2

There is an article on Single.TryParse over at MSDN with this example code: http://msdn.microsoft.com/en-us/library/26sxas5t%28v=vs.100%29.aspx

// Parse a floating-point value with a thousands separator.
value = "1,643.57";
if (Single.TryParse(value, out number))
    Console.WriteLine(number);
else
    Console.WriteLine("Unable to parse '{0}'.", value);

Problem is in the article the TryParse returns true and the string is converted, but when I try it, it's false. How do I fix this?


UPD: To simplify parsing, these two lines can be used:

NumberStyles style = System.Globalization.NumberStyles.Any;
CultureInfo culture = CultureInfo.InvariantCulture;

This setting allows for negative floats and strings with leading and trailing space characters to be parsed.

user1306322
  • 8,561
  • 18
  • 61
  • 122
  • 2
    Is your system culture one that uses `,` as a thousands separator and `.` as a decimal separator? In Germany for example, that is not the case. – Eric J. Aug 28 '12 at 18:39
  • Your problem is probably the culture. Your number can only be parsed if the culture uses `.` as decimal seperator. So you should use an overload that takes a culture, and specify `InvariantCulture`. – CodesInChaos Aug 28 '12 at 18:40
  • Oh, so it's the separator. For a moment there I thought some kind of bad karma is involved. – user1306322 Aug 28 '12 at 18:44
  • 2
    Clearly the problem is you're not in America. Time to move. – Servy Aug 28 '12 at 18:46

2 Answers2

4

you need to set culture like this

using System.Globalization;

string value = "1345,978";
NumberStyles style = System.Globalization.NumberStyles.AllowDecimalPoint;
CultureInfo 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);

from msdn : Single.TryParse Method (String, NumberStyles, IFormatProvider, Single%)

or

float usedAmount;
// try parsing with "fr-FR" first
bool success = float.TryParse(inputUsedAmount.Value,
                              NumberStyles.Float | NumberStyles.AllowThousands,
                              CultureInfo.GetCultureInfo("fr-FR"),
                              out usedAmount);

if (!success)
{
    // parsing with "fr-FR" failed so try parsing with InvariantCulture
    success = float.TryParse(inputUsedAmount.Value,
                             NumberStyles.Float | NumberStyles.AllowThousands,
                             CultureInfo.InvariantCulture,
                             out usedAmount);
}

if (!success)
{
    // parsing failed with both "fr-FR" and InvariantCulture
}

Answered over here : C# float.tryparse for French Culture

Community
  • 1
  • 1
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
1

You have problem with your culture about : , character

You can use CultureInvariant in your string

Patrick D'Souza
  • 3,491
  • 2
  • 22
  • 39
Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51