不同的小数符号C#

时间:2015-10-23 04:13:29

标签: c#

我有程序将它生成的双打保存到文件并读取文件以便下次运行。 问题是当它想要在不同的窗口中第一次读取文件时,它无法在不同的窗口中管理小数符号,有些窗口使用像“。”这样的符号。其中一些使用'/'作为分隔符。 我该如何处理这个问题?

加载部分:

private void Load( string file )
    {
        if ( File.Exists( file ) )
        {
            StreamReader reader = new StreamReader( file );
            string line = reader.ReadLine();
              //I think i should use IFormatProvider here.
            m_GamesTrained = int.Parse( line );
              //Some code here!but irrelevant to topic

            reader.Close();
        }       
    }

并保存部分:

private void Save( string file )
    {

                StreamWriter writer = new StreamWriter( file, false );

                m_LastGamesTrainedSave = m_GamesTrained;
                writer.WriteLine( m_GamesTrained.ToString() );

                float[] contactWeights = m_ContactNetwork.GetWeights();
                for ( int i = 0; i < contactWeights.Length; i++ )
                {
                  //I think i should use IFormatProvider here too
                    writer.WriteLine( contactWeights[i].ToString() );
                }


                writer.Close();

    }

1 个答案:

答案 0 :(得分:2)

using System.Globalization;

用于保存数据:

number.ToString(CultureInfo.InvariantCulture)

预读数字:

int.Parse(number,CultureInfo.InvariantCulture);
//you can use float instead of int