15

I have a user input which can contain float values ranging from : 3.06 OR 3,06 The culture we are in is French and thus when the user inputs 3.06 and I run a float.tryParse over this value it does not get converted to 3.06 into a new variable (type float)

// inputUsedAmount.Value from UI is : 3.06
float usedAmount = 0.0f;
float.TryParse(inputUsedAmount.Value, out usedAmount);
// returns false

I can simply do a replace on the amount entered from UI from "." to ",", but is there a graceful/better way of doing this through Culture ? Thanks

Murtaza Mandvi
  • 10,708
  • 23
  • 74
  • 109
  • 1
    It seems reasonable -at least to me- to reject "3.06" as valid input. In France (you might even say, Europe) "3,06" is a valid number and "3.06" simply is not. Does your application also support dates in the following format "MM/DD/YYYY" or do-you/would-you reject such a date for having an invalid format? I think the latter, and quite reasonably so. – Captain Sensible Apr 27 '10 at 16:39

3 Answers3

19

You can use the overload that takes a format provider. You can pass through a French culture info:

string value;
NumberStyles style;
CultureInfo culture;
double number;

value = "1345,978";
style = NumberStyles.AllowDecimalPoint;
culture = CultureInfo.CreateSpecificCulture("fr-FR");
if (Double.TryParse(value, style, culture, out number))
   Console.WriteLine("Converted '{0}' to {1}.", value, number);
else
   Console.WriteLine("Unable to convert '{0}'.", value);
// Displays:
//       Converted '1345,978' to 1345.978.
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • 2
    Be aware that this code uses `NumberStyles.AllowDecimalPoint`. There are some strings that you won't be able to parse if you only specify `AllowDecimalPoint`. For example, negative numbers won't work. By default, `TryParse` uses `NumberStyles.Float | NumberStyles.AllowThousands`. `NumberStyle.Float` itself is a combination of `Integer | AllowExponent | AllowDecimalPoint`. – Rudey Oct 26 '18 at 18:53
6
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
}
LukeH
  • 263,068
  • 57
  • 365
  • 409
0

You can pass culture inside TryParse method:

public static bool TryParse(string s, NumberStyles style, IFormatProvider provider, out float result);
Andrew Bezzub
  • 15,744
  • 7
  • 51
  • 73
  • 1
    Yes but how do i decide which culture to pass since the UI value can either be "." Or "," – Murtaza Mandvi Apr 27 '10 at 16:05
  • You need to ask the user (or in some other way determine) which locale they are in. Most systems have a setting for determining the current culture. Is your question, then, the best way to devise such a system or determine the client's culture? – BlueMonkMN Apr 27 '10 at 16:17