0

Here is a List< string > lstA;

lstA may contains value(datatype string) like: 1st example

1

1.00

1000.01

1,002.98

.........

Or listA may contain value like: 2nd example

1

1,00

1000,01

1.002,98

.........

i am trying to pass this list to a method and it will return me a string "german" or "us"

I am trying this:

private static string ReturnCulture(List<string> lst)
    {
        string cull = "";
        foreach (string s in lst)
        {
            if (s.Contains(".") && s.Contains(","))
            {
                cull = "german";
                break;
            }
            else if (!s.Contains(".") && s.Contains(","))
            {
                cull = "german";
            }
            else if (s.Contains(".") && !s.Contains(","))
            {
                cull = "us";
                break;
            }
            else if (!s.Contains(".") && !s.Contains(","))
            {
                continue;
            }
        }

        return cull;
    }

this works fine as for first 3 value. but when i get (1,002.98) or ("1.002,98") this both fullfils for german condition.

How to solve this? can globalization class easily detect which culture it is? the given example of lstA is realy random. but it will be either as 1st example or the 2nd.

  • 2
    What happens when you get two cultures that share some of the same symbols? – Lloyd Mar 25 '13 at 13:38
  • i have to think about two symbols here "." and ",". In my last 5th line i cleared the scene which i worried about. –  Mar 25 '13 at 13:41
  • What is your final goal? Why do you need to parse numbers to culture? – evgenyl Mar 25 '13 at 13:47
  • i have to do some calculations with the values of this list and i have to do this in german format. but input is not always in german. –  Mar 25 '13 at 13:49
  • So, i'd suggest you to change your post to "how can I parse numbers from different cultures" - because when you will receive invariant numbers, you can easaly format them to german. – evgenyl Mar 25 '13 at 13:54
  • can i change my title now? –  Mar 25 '13 at 13:57
  • His issue though is that he doesn't know the culture. He seems to know that it's either german or us, but he can't tell for sure. And on a value such as `1.000`, you can't say which it is for certain. `1.000` is a valid number in both cultures, but have different actual values in each. I might suggest using `Double.TryParse` with a german culture and a us culture. Then if only one of them works, you can use that one. If both work.... well you're going to have to pick a default... – Tyler Lee Mar 25 '13 at 14:05
  • @user1865670 yes, you can change the title or edit your post. Did you try to use double.parse? – evgenyl Mar 25 '13 at 14:17

3 Answers3

0

If you are trying to parse numbers of only several cultures, may be you'll find usefull using double.parse with culture format parameter. See Double parse with culture format

Community
  • 1
  • 1
evgenyl
  • 7,837
  • 2
  • 27
  • 32
0

You cannot definitively create a method that given a set of strings will be able to tell you whether the strings are formatted as English or German. You can write a method that will tell you whether all the strings can be parsed as English or German. Since you expect parsing failures, you should use Double.TryParse as in:

private static bool AreAllStringsValidNumberRepresenationsForCulture(CultureInfo ci, List<string> lst)
{
    foreach (string s in lst)
    {
        double number;
        if (!Double.TryParse(s, NumberStyles.Any, ci, out number)
        {
            return false;
        }
    }
    return true;
}

It's always better to take explicit input instead of relying on a heuristic, so if there is any way to force a standard number representation, you'd be better off.

Eric MSFT
  • 3,246
  • 1
  • 18
  • 28
0

You can try parsing them using both culture,

Eg,

public static string GetCulture(this string value)
{
    var doubleValue = default(double);
    var germanCultureInfo = new CultureInfo("de-DE");
    var englishCultureInfo = new CultureInfo("en-US");
    if (double.TryParse(value, NumberStyles.AllowDecimalPoint, germanCultureInfo, out doubleValue))
    {
        return "german";
    }
    else if (double.TryParse(value, NumberStyles.AllowDecimalPoint, englishCultureInfo, out doubleValue))
    {
       return "us";
    }
    return string.Empty; ;
}
Akbar Badhusha
  • 2,415
  • 18
  • 30