Advertisement
kerrubin

[C#] Formatage des nombres et culture

Jan 23rd, 2012
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.11 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4.  
  5. namespace Helpers
  6. {
  7.     public class Format
  8.     {
  9.         public static NumericFormat Numeric { get { return NumericFormat.Instance; } }
  10.         public static DateFormat Date { get { return DateFormat.Instance; } }
  11.  
  12.         public static void Initialize(
  13.             String emptyString,
  14.             CultureInfo defaultCulture,
  15.             CultureInfoCollection collection)
  16.         {
  17.             if (defaultCulture == null)
  18.                 throw new ArgumentNullException("defaultCulture");
  19.             if (collection == null)
  20.                 throw new ArgumentNullException("collection");
  21.  
  22.             collection.Add("Default", defaultCulture);
  23.  
  24.             Numeric.Initialize(emptyString, collection);
  25.             Date.Initialize(emptyString, collection);
  26.         }
  27.     }
  28.     public abstract class TypeFormat
  29.     {
  30.         public String EmptyString { get; set; }
  31.         public CultureInfo DefaultCulture { get; set; }
  32.  
  33.         internal abstract void Initialize(
  34.             String emptyString,
  35.             CultureInfoCollection collection);
  36.     }
  37.     public class NumericFormat
  38.         : TypeFormat
  39.     {
  40.         #region Singleton
  41.         private static readonly NumericFormat instance = new NumericFormat();
  42.         internal static NumericFormat Instance { get { return instance; } }
  43.         static NumericFormat() { }
  44.         private NumericFormat() { }
  45.         #endregion
  46.  
  47.         #region Initialization
  48.         internal override void Initialize(
  49.             String emptyString,
  50.             CultureInfoCollection collection)
  51.         {
  52.             EmptyString = emptyString;
  53.             Collection = collection;
  54.         }
  55.         #endregion
  56.  
  57.         #region Properties
  58.         private CultureInfoCollection Collection { get; set; }
  59.         #endregion
  60.  
  61.         #region Public Methods
  62.         public String NumberFormatDecimals(Double numeric, String culture = null)
  63.         {
  64.             return numeric.ToString("N", Collection.GetCulture(culture).NumberFormat);
  65.         }
  66.         public String NumberFormatDecimals(Double? numeric)
  67.         {
  68.             if (numeric.HasValue)
  69.                 return NumberFormatDecimals(numeric);
  70.             else return EmptyString;
  71.         }
  72.         public String NumberFormat(Double numeric, String culture = null)
  73.         {
  74.             return numeric.ToString("N0", Collection.GetCulture(culture).NumberFormat);
  75.         }
  76.         public String NumberFormat(Double? numeric)
  77.         {
  78.             if (numeric.HasValue)
  79.                 return NumberFormat(numeric);
  80.             else return EmptyString;
  81.         }
  82.  
  83.         public String NumberFormatDecimals(Single numeric, String culture = null)
  84.         {
  85.             return numeric.ToString("N", Collection.GetCulture(culture).NumberFormat);
  86.         }
  87.         public String NumberFormatDecimals(Single? numeric)
  88.         {
  89.             if (numeric.HasValue)
  90.                 return NumberFormatDecimals(numeric);
  91.             else return EmptyString;
  92.         }
  93.         public String NumberFormat(Single numeric, String culture = null)
  94.         {
  95.             return numeric.ToString("N0", Collection.GetCulture(culture).NumberFormat);
  96.         }
  97.         public String NumberFormat(Single? numeric)
  98.         {
  99.             if (numeric.HasValue)
  100.                 return NumberFormat(numeric);
  101.             else return EmptyString;
  102.         }
  103.  
  104.         public String NumberFormat(Int16 numeric, String culture = null)
  105.         {
  106.             return numeric.ToString("N0", Collection.GetCulture(culture).NumberFormat);
  107.         }
  108.         public String NumberFormat(Int16? numeric)
  109.         {
  110.             if (numeric.HasValue)
  111.                 return NumberFormat(numeric);
  112.             else return EmptyString;
  113.         }
  114.         public String NumberFormat(Int32 numeric, String culture = null)
  115.         {
  116.             return numeric.ToString("N0", Collection.GetCulture(culture).NumberFormat);
  117.         }
  118.         public String NumberFormat(Int32? numeric)
  119.         {
  120.             if (numeric.HasValue)
  121.                 return NumberFormat(numeric);
  122.             else return EmptyString;
  123.         }
  124.         public String NumberFormat(Int64 numeric, String culture = null)
  125.         {
  126.             return numeric.ToString("N0", Collection.GetCulture(culture).NumberFormat);
  127.         }
  128.         public String NumberFormat(Int64? numeric)
  129.         {
  130.             if (numeric.HasValue)
  131.                 return NumberFormat(numeric);
  132.             else return EmptyString;
  133.         }
  134.  
  135.         public String PercentFormatDecimals(Double percent, String culture = null)
  136.         {
  137.             return percent.ToString("P", Collection.GetCulture(culture).NumberFormat);
  138.         }
  139.         public String PercentFormatDecimals(Double? percent)
  140.         {
  141.             if (percent.HasValue)
  142.                 return PercentFormatDecimals(percent);
  143.             else return EmptyString;
  144.         }
  145.         public String PercentFormat(Double percent, String culture = null)
  146.         {
  147.             return percent.ToString("P0", Collection.GetCulture(culture).NumberFormat);
  148.         }
  149.         public String PercentFormat(Double? percent)
  150.         {
  151.             if (percent.HasValue)
  152.                 return PercentFormat(percent);
  153.             else return EmptyString;
  154.         }
  155.  
  156.         public String CurrencyFormat(Double currency, String culture = null)
  157.         {
  158.             return currency.ToString("C0", Collection.GetCulture(culture).NumberFormat);
  159.         }
  160.         public String CurrencyFormat(Double? currency)
  161.         {
  162.             if (currency.HasValue)
  163.                 return CurrencyFormat(currency.Value);
  164.             else return EmptyString;
  165.         }
  166.         public String CurrencyFormatDecimals(Double currency, String culture = null)
  167.         {
  168.             return currency.ToString("C", Collection.GetCulture(culture).NumberFormat);
  169.         }
  170.         public String CurrencyFormatDecimals(Double? currency)
  171.         {
  172.             if (currency.HasValue)
  173.                 return CurrencyFormatDecimals(currency.Value);
  174.             else return EmptyString;
  175.         }
  176.         public String CurrencyFormat(Int32 currency, String culture = null)
  177.         {
  178.             return currency.ToString("C0", Collection.GetCulture(culture).NumberFormat);
  179.         }
  180.         public String CurrencyFormat(Int32? currency)
  181.         {
  182.             if (currency.HasValue)
  183.                 return CurrencyFormat(currency.Value);
  184.             else return EmptyString;
  185.         }
  186.         public String CurrencyFormat(Int64 currency, String culture = null)
  187.         {
  188.             return currency.ToString("C0", Collection.GetCulture(culture).NumberFormat);
  189.         }
  190.         public String CurrencyFormat(Int64? currency)
  191.         {
  192.             if (currency.HasValue)
  193.                 return CurrencyFormat(currency.Value);
  194.             else return EmptyString;
  195.         }
  196.         public String CurrencyFormat(Single currency, String culture = null)
  197.         {
  198.             return currency.ToString("C0", Collection.GetCulture(culture).NumberFormat);
  199.         }
  200.         public String CurrencyFormat(Single? currency)
  201.         {
  202.             if (currency.HasValue)
  203.                 return CurrencyFormat(currency.Value);
  204.             else return EmptyString;
  205.         }
  206.         public String CurrencyFormatDecimals(Single currency, String culture = null)
  207.         {
  208.             return currency.ToString("C", Collection.GetCulture(culture).NumberFormat);
  209.         }
  210.         public String CurrencyFormatDecimals(Single? currency)
  211.         {
  212.             if (currency.HasValue)
  213.                 return CurrencyFormatDecimals(currency.Value);
  214.             else return EmptyString;
  215.         }
  216.         #endregion
  217.     }
  218.     public class DateFormat
  219.         : TypeFormat
  220.     {
  221.         #region Singleton
  222.         private static readonly DateFormat instance = new DateFormat();
  223.         internal static DateFormat Instance { get { return instance; } }
  224.         static DateFormat() { }
  225.         private DateFormat() { }
  226.         #endregion
  227.  
  228.         #region Initialization
  229.         internal override void Initialize(
  230.             String emptyString,
  231.             CultureInfoCollection collection)
  232.         {
  233.             EmptyString = emptyString;
  234.             Collection = collection;
  235.         }
  236.         #endregion
  237.  
  238.         #region Properties
  239.         private CultureInfoCollection Collection { get; set; }
  240.         #endregion
  241.  
  242.         #region Public Methods
  243.         public String ShortDate(DateTime date, String culture = null)
  244.         {
  245.             return date.ToString("d", Collection.GetCulture(culture).DateTimeFormat);
  246.         }
  247.         public String ShortDate(DateTime? date)
  248.         {
  249.             if (date.HasValue)
  250.                 return LongDate(date.Value);
  251.             else return EmptyString;
  252.         }
  253.         public String LongDate(DateTime date, String culture = null)
  254.         {
  255.             return date.ToString("D", Collection.GetCulture(culture).DateTimeFormat);
  256.         }
  257.         public String LongDate(DateTime? date)
  258.         {
  259.             if (date.HasValue)
  260.                 return LongDate(date.Value);
  261.             else return EmptyString;
  262.         }
  263.  
  264.         public String ShortTime(DateTime date, String culture = null)
  265.         {
  266.             return date.ToString("t", Collection.GetCulture(culture).DateTimeFormat);
  267.         }
  268.         public String ShortTime(DateTime? date)
  269.         {
  270.             if (date.HasValue)
  271.                 return ShortTime(date.Value);
  272.             else return EmptyString;
  273.         }
  274.         public String LongTime(DateTime date, String culture = null)
  275.         {
  276.             return date.ToString("T", Collection.GetCulture(culture).DateTimeFormat);
  277.         }
  278.         public String LongTime(DateTime? date)
  279.         {
  280.             if (date.HasValue)
  281.                 return LongTime(date.Value);
  282.             else return EmptyString;
  283.         }
  284.  
  285.         public String FullDateTime(DateTime date, String culture = null)
  286.         {
  287.             return date.ToString("F", Collection.GetCulture(culture).DateTimeFormat);
  288.         }
  289.         public String FullDateTime(DateTime? date)
  290.         {
  291.             if (date.HasValue)
  292.                 return FullDateTime(date.Value);
  293.             else return EmptyString;
  294.         }
  295.         #endregion
  296.     }
  297.     public class CultureInfoCollection
  298.         : Dictionary<String, CultureInfo>
  299.     {
  300.         public CultureInfo GetCulture(String name)
  301.         {
  302.             try
  303.             {
  304.                 if(String.IsNullOrEmpty(name))
  305.                     return this["Default"];
  306.  
  307.                 if (name.Equals("Current", StringComparison.InvariantCultureIgnoreCase))
  308.                     return System.Threading.Thread.CurrentThread.CurrentCulture;
  309.  
  310.                 if (ContainsKey(name))
  311.                     return this[name];
  312.                 else
  313.                 {
  314.                     CultureInfo wanted = new CultureInfo(name);
  315.                     Add(wanted.Name, wanted);
  316.                     return wanted;
  317.                 }
  318.             }
  319.             catch
  320.             {
  321.                 return this["Default"];
  322.             }
  323.         }
  324.     }
  325. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement