Convert.ToDouble geht in einenm Projekt im anderen nicht, warum?

RobinJ 276 Reputation points
2020-11-06T10:32:24.267+00:00

Hallo zusammen,

In meinem einen C# Projekt funktioniert das double Tage_double = Convert.ToDouble(Tage_string);
problemlos und im anderen Projekt funktioniert es nicht obwohl ich den Code kopiert habe.

Kann mir jemand helfen?

Mit freundlichen Grüssen
RobinJ

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,760 questions
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,321 Reputation points
    2020-11-07T05:37:45.593+00:00

    Hi Robin,
    try following console demo:

    using System;
    using System.Globalization;
    using System.Windows.Forms;
    
    namespace ConsoleApp1
    {
      class Program28
      {
        static void Main(string[] args)
        {
          try
          {
            (new Demo()).Execute();
          }
          catch (Exception ex) { Console.WriteLine(ex.ToString()); }
          Console.WriteLine("Continue enter key");
          Console.ReadKey();
        }
    
        internal class Demo
        {
          internal void Execute()
          {
            string Tage_string = "1,234,567.89";
    
            // english OS
            Application.CurrentCulture = CultureInfo.InvariantCulture;
            try
            {
              double Tage_double = Convert.ToDouble(Tage_string);
              Console.WriteLine(Tage_double);
            }
            catch (Exception ex) { Console.WriteLine(ex.Message); }
    
            // german OS
            Application.CurrentCulture = CultureInfo.GetCultureInfo("de-de");
            try
            {
              double Tage_double = Convert.ToDouble(Tage_string);
              Console.WriteLine(Tage_double);
            }
            catch (Exception ex) { Console.WriteLine(ex.Message); }
    
            // german OS with IFormatProvider
            Application.CurrentCulture = CultureInfo.GetCultureInfo("de-de");
            try
            {
              double Tage_double = Convert.ToDouble(Tage_string, CultureInfo.InvariantCulture);
              Console.WriteLine(Tage_double);
            }
            catch (Exception ex) { Console.WriteLine(ex.Message); }
    
          }
        }
      }
    }
    
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.