Csharp/C Sharp by API/System.Globalization/NumberFormatInfo

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

new NumberFormatInfo()

  
using System;
using System.Globalization;
using System.Text;
public class NumParsingApp {
    public static void Main(string[] args) {
        string u = "AA -1,234,567.890  ";
        NumberFormatInfo ni = new NumberFormatInfo();
        ni.CurrencySymbol = "AA";
        double h = Double.Parse(u, NumberStyles.Any, ni);
        Console.WriteLine("h = {0:F}", h);
    }
}


NumberFormatInfo.CurrencyGroupSeparator

  
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
using System.Reflection;
public class MainClass{
   public static void Main(){
        string[] money = new string[] { "$0.99", "$0,99", "$1000000.00", "$10.25", "$90,000.00", "$90.000,00", "$1,000,000.00", "$1,000000.00" };
        NumberFormatInfo info = CultureInfo.CurrentCulture.NumberFormat;
        Regex moneyint = new Regex(String.Format(@"\{0}(\d{{1,3}}\{0})*\d+\{1}\d{{2}}",
            info.CurrencyGroupSeparator, info.CurrencyDecimalSeparator));
        foreach (string m in money)
        {
            Console.WriteLine("{0}: {1}", m, moneyint.IsMatch(m));
        }
   }
}


NumberFormatInfo.CurrencySymbol

  
using System;
using System.Globalization;
class MainClass
{
  public static void Main() 
  {
    CultureInfo ci = new CultureInfo("nl-BE");
    NumberFormatInfo nfi = ci.NumberFormat;
    Console.WriteLine("Currency symbol: " + nfi.CurrencySymbol);
    Console.WriteLine("Decimal seperator: " + nfi.NumberDecimalSeparator);
  }
}


NumberFormatInfo.NumberDecimalSeparator

  
using System;
using System.Globalization;
class MainClass
{
  public static void Main() 
  {
    CultureInfo ci = new CultureInfo("nl-BE");
    NumberFormatInfo nfi = ci.NumberFormat;
    Console.WriteLine("Currency symbol: " + nfi.CurrencySymbol);
    Console.WriteLine("Decimal seperator: " + nfi.NumberDecimalSeparator);
  }
}


NumberFormatInfo.NumberGroupSeparator

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

class Class1 {
  static void Main(string[] args) {
      CultureInfo MyCulture = new CultureInfo(Thread.CurrentThread.CurrentUICulture.LCID);
      NumberFormatInfo Vnf = new NumberFormatInfo();
      Console.WriteLine(123456.ToString("c", MyCulture));
      Vnf = MyCulture.NumberFormat;
      Vnf.CurrencyDecimalSeparator = ",";
      Vnf.CurrencyGroupSeparator = ".";
      Vnf.NumberDecimalSeparator = ",";
      Vnf.NumberGroupSeparator = ".";
      MyCulture.NumberFormat = Vnf;
      Console.WriteLine(123456.ToString("C", MyCulture));
   }
}