none
Convert string to int or double, decimal RRS feed

  • Question

  • Hello,
    What's the right way to do it?
    Works, but maybe not the best way.
    string t2 = countToAnaylse.Replace(".", "").Replace(",", "");
    Nothing works.

    My trials.

    // -----------------------------------------------------------
    
    string countToAnaylse = "7.765"; 
    
    int helpCountMustBe = 0;
    
    //CultureInfo.InvariantCulture
    // helpCountMustBe = int.Parse(countToAnaylse, new CultureInfo("de-DE"));
    NumberFormatInfo num = new NumberFormatInfo();
    num.NumberDecimalSeparator = ",";
    num.NumberGroupSeparator = "-"; // ".";
    string t1  = String.Format(num, "{0}", countToAnaylse);
    string t2 = countToAnaylse.Replace(".", "").Replace(",", "");
    
    //try
    //{
    //    helpCountMustBe = int.Parse(countToAnaylse, CultureInfo.InvariantCulture);
    //}
    //catch (Exception eCult)
    //{
    //    Trace.Writeline($"{LOG}[Scan] '{eCult.Message}' ");
    //}
    
    //if (helpCountMustBe == 0)
    //{
    //    helpCountMustBe = int.Parse(countToAnaylse, new CultureInfo("de-DE"));
    //}
    I want no dot!   As Replace!
    Best regards Markus

    Saturday, October 12, 2019 10:32 AM

Answers

  • Try something like this

    string countToAnaylse = "7.765";
     
    int output;
     
    int.TryParse(countToAnaylse, System.Globalization.NumberStyles.Any, new CultureInfo("de-DE"), out output);
    

    Saturday, October 12, 2019 12:23 PM