none
Convert.ToDouble is not working right? RRS feed

  • Question

  • Hi,

    I have this Code:

    string FirmaGeoX = "55.24562";
    double dGeoX = Convert.ToDouble(FirmaGeoX);
    

    The Result in the dGeox = 5524562

    Without the "."

    What is here wrong?   ( I Need the "." in the dGeoX Field! )

    Best Regards

    Bernd

    Friday, January 11, 2019 1:50 PM

Answers

  • I got it :-)

                double dGeoX = Convert.ToDouble(FirmaGeoX, CultureInfo.InvariantCulture);
    

    Many Thanks!

    • Marked as answer by Bernd Riemke Friday, January 11, 2019 3:33 PM
    Friday, January 11, 2019 3:33 PM

All replies

  • Using C# 6

    string FirmaGeoX = "55.24562";
    if (double.TryParse(FirmaGeoX, out var result))
    {
        MessageBox.Show(result.ToString());
    }

    Earlier versions of C#

    string FirmaGeoX = "55.24562";
    double result = 0;
    if (double.TryParse(FirmaGeoX, out result))
    {
        MessageBox.Show(result.ToString());
    }


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, January 11, 2019 1:55 PM
    Moderator
  • Use CultureInfo
    Friday, January 11, 2019 1:59 PM
  • Sorry but my MessageBox show this:

    Friday, January 11, 2019 3:04 PM
  • "523962" without a "."

    I use Visual Studio 2017

    Friday, January 11, 2019 3:07 PM
  • I use this now:

                var ResultX = Single.Parse(FirmaGeoX, CultureInfo.InvariantCulture.NumberFormat);
                double dGeoX = ResultX;
    
    in ResultX is the Right number but in dGeoX NOT !?

    Friday, January 11, 2019 3:18 PM
  • I got it :-)

                double dGeoX = Convert.ToDouble(FirmaGeoX, CultureInfo.InvariantCulture);
    

    Many Thanks!

    • Marked as answer by Bernd Riemke Friday, January 11, 2019 3:33 PM
    Friday, January 11, 2019 3:33 PM
  • The dot is the decimal separator in US and some other places. It is the thousands separator in other places. My guess is that your locale is using , for the decimal separator and . for the thousands separator. Check your CultureInfo.Current to see what the separator is being using.

    Michael Taylor http://www.michaeltaylorp3.net

    Friday, January 11, 2019 3:48 PM
    Moderator