Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different decimal symbol C#

Tags:

c#

I have program that save doubles it produce to file and read the file for next run. Problem is when it want to read file for first time in different windows, it could not manage decimal symbol in different windows, some windows use symbol like '.' and some of them use '/' as separator. How can i handle this problem?

load section:

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();
        }       
    }

and save section:

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();

    }
like image 800
zex_rectooor Avatar asked Aug 17 '26 13:08

zex_rectooor


1 Answers

using System.Globalization;

For saving data:

number.ToString(CultureInfo.InvariantCulture)

Fore reading numbers:

int.Parse(number,CultureInfo.InvariantCulture);
//you can use float instead of int
like image 198
zex_rectooor Avatar answered Aug 20 '26 01:08

zex_rectooor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!