2

Hi i need to convert a decimal value based on the culture info. For EG :

If en-US culture the decimal value will be like 21.56 and in tr-TR which is turkey culture info , here the same value will be as 21,56

Si my requirement is to whatever the decimal value , but i need to get in en-US as default. I need my decimal value seperated by dot and i dont want the decimal value of comma.

I tried converting using the below code

                CultureInfo userCulture = new CultureInfo("tr-TR");
                string fromVisioAPI = "35,2083";
                string display = double.Parse(fromDb, userCulture).ToString();

and here the output was "35.2083" which was i expected, but here am hardcoding the tr-TR value and i dunno how many culture does the same comma and dot difference.

This was a normal replacement and i need to be a proper culture conversion

So what's the best way of converting comma decimal values to dot decimal values using culture info..?

Community
  • 1
  • 1
Dah Sra
  • 4,107
  • 3
  • 30
  • 69
  • 4
    You can't, in general. "21,123" might mean "twenty-one thousand, one hundred and 23" or it might mean "just a bit more than twenty-one". That depends on the culture. You need to know the culture in order to understand the value. – Jon Skeet Nov 09 '15 at 06:46
  • @JonSkeet so its betting to get in a string and replace comma with dot right..? – Dah Sra Nov 09 '15 at 06:47
  • 1
    I don't know what you mean by "its betting" - but I'm saying that unless you know the culture, you *can't* reliably understand the value. You definitely should *not* just replace commas with dots, because that can change the meaning. – Jon Skeet Nov 09 '15 at 06:49
  • sorry its better , not betting – Dah Sra Nov 09 '15 at 06:54
  • Okay, so the answer is basically "no, don't do that". – Jon Skeet Nov 09 '15 at 06:56
  • Given the name `fromDb`: why are you storing decimal values as strings in the database in the first place? Store them as a numeric type and you won't have to worry about decimal separators. –  Nov 09 '15 at 07:07
  • @hvd i am getting those values from visio API and here in API if i get in double also am getting decimal values with comma only.! – Dah Sra Nov 09 '15 at 07:11
  • Okay. It would greatly surprise me if it returns numbers as strings, unless there is at least documentation on which string format it uses, but not actually knowing the API, I guess I cannot rule it out. –  Nov 09 '15 at 07:22
  • @hvd `Return Value: Double` looks pretty straighforward to me. The sample even assigns it to a double variable (in VBA). – Luaan Nov 09 '15 at 09:20

4 Answers4

1

You need to know the culture info for the data to parse. If that was static, you could simply hard code it or configure it in the app.config. Otherwise you could use CultureInfo.CurrentCulture.

To convert the values back to string you should use the ToString overload with a specific culture info:

var visioApiCulture = 
    new CultureInfo(ConfigurationManager.AppSettings["VisioApiCulture"]);

or

var visioApiCulture = CultureInfo.CurrentCulture;

-

string fromVisioApi = "35,2083";

string display = double
    .Parse(fromVisioApi, visioApiCulture)
    .ToString(CultureInfo.InvariantCulture);
matthias.lukaszek
  • 2,200
  • 1
  • 23
  • 33
0

Assuming the Visio API is running on the same computer as your code, then the Windows culture info that Visio is using is very likely the same as what your program is getting as its default. You can get the current culture from the CultureInfo.CurrentCulture property: https://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.currentculture(v=vs.110).aspx

modal_dialog
  • 733
  • 5
  • 12
0

It think the below code can help you ..

public double? ConvertStringToDouble(string strDoubleValue)
{
    //Checking null
    if (strDoubleValue == null)
    {
        return null;
    }

    //Making trim
    strDoubleValue = strDoubleValue.Trim();

    //Checking empty
    if (strDoubleValue == string.Empty)
    {
        return null;
    }

    //If the amout treat dot(.) as decimal separator
    if (strDoubleValue.IndexOf('.')!=-1)
    {
        //If multiple . is present then the amount is invaid
        if (strDoubleValue.Count(o=>o=='.')>1)
        {
            return null;
        }

        //removing thousand separators
        //it might not be needed
        strDoubleValue = strDoubleValue.Replace(",", "");

        return ConvertPlainStringToDouble(strDoubleValue);
    }


    //If the amout treat dot(,) as decimal separator
    //then it must not use ',' as thousand separator
    if (strDoubleValue.Count(o => o == ',') > 1)
    {
        //removing thousand separators
        //it might not be needed
        strDoubleValue = strDoubleValue.Replace(",", "");
        return ConvertPlainStringToDouble(strDoubleValue);                
    }

    //Here will be logic that the string contains single comma , is treated here as 
    //deciaml separator or comma separator

    //int charCountBeforeComma = strDoubleValue.IndexOf(',');
    //int charCountAfterComma = strDoubleValue.Length - (charCountBeforeComma + 1);

    ////If charCountAfterComma is not in 3rd position than
    ////the comma cannot be thousand separator example: 458,5896
    //if (charCountAfterComma!=3)
    //{
    //    //removing thousand separators
    //    //it might not be needed
    //    strDoubleValue = strDoubleValue.Replace(",", ".");
    //    return ConvertPlainStringToDouble(strDoubleValue);   
    //}

    //if string having more than 3 char before comma like 4589,548
    //it means no thousand separator used else the amount should represent like this 4,589,548
    //you can use below code 
    //if (charCountBeforeComma>3)
    //{
    //    //removing thousand separators
    //    //it might not be needed
    //    strDoubleValue = strDoubleValue.Replace(",", "");
    //    return ConvertPlainStringToDouble(strDoubleValue);   
    //}

    //if all above missed than i am sorry              
    //it means the string is like 458,458 or 58,458 format
    //you need to put some logical condition here 
    //??????

}

private Double? ConvertPlainStringToDouble(string strPlainDoubleValue)
{            
    Double amount;
    if (Double.TryParse(strPlainDoubleValue, out amount))
    {
        return amount;
    }

    return null;
}

I tried to attend all logical condition to resolve the issue .. but in end .. there is something nee d to fill by you .. :)

Moumit
  • 8,314
  • 9
  • 55
  • 59
  • 1
    This is still a guessing game, and it only considers decimal points and thousands separators anyway - there's plenty of other things that can break the parsing. And the very first thing you assume is that if there is a `.`, it means it must be the decimal point. That's simply incorrect - `1.123.456,50` is a perfectly valid number - including as esoteric languages as *german* (in fact, there's *92 cultures* in .NET using that format!). – Luaan Nov 09 '15 at 09:26
0

You should use the universal format properties in the API - those don't depend on the culture setting.

For example, to get the formula of a cell, use FormulaU - this will keep all the numbers in the . means decimal point, , means thousands separator format.

Using Result should also work, since it's supposed to return a double, not a string - if you're getting a string, either you're using an incorrect interop library, or you're doing something wrong. You can use ResultStrU as a work-around, again, to get an universal format string.

Luaan
  • 62,244
  • 7
  • 97
  • 116