C# Currency Example

← All NMath Code Examples

 

using System;
using System.IO;
using System.Threading;
using System.Globalization;

using CenterSpace.NMath.Core;

namespace CenterSpace.NMath.Examples.CSharp
{
  /// <summary>
  /// A .NET example in C# showing how to read and write vectors of currencies.
  /// </summary>
  class CurrencyExample
  {
    static void Main( string[] args )
    {
      Console.WriteLine();

      // American
      Thread.CurrentThread.CurrentCulture = new CultureInfo( "en-US" );

      // Create a vector of US currency values.
      var v = new DoubleVector( "$4.30 $0.08 ($5.87)",
         NumberStyles.Number | NumberStyles.AllowCurrencySymbol | NumberStyles.AllowParentheses );

      // Dollars
      Console.Out.WriteLine( v.ToString( "C" ) );

      // Convert it to Pounds
      v = v * 0.6823; // exchange rate

      // Round to two decimal places
      v = NMathFunctions.Round( v, 2 );

      // Im British.
      Thread.CurrentThread.CurrentCulture = new CultureInfo( "en-GB" );
      Console.Out.WriteLine( v.ToString( "C" ) );

      Console.WriteLine();
      Console.WriteLine( "Press Enter Key" );
      Console.Read();

    } // Main

  }// class

}// namespace

← All NMath Code Examples
Top