Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- namespace Helpers
- {
- public class Format
- {
- public static NumericFormat Numeric { get { return NumericFormat.Instance; } }
- public static DateFormat Date { get { return DateFormat.Instance; } }
- public static void Initialize(
- String emptyString,
- CultureInfo defaultCulture,
- CultureInfoCollection collection)
- {
- if (defaultCulture == null)
- throw new ArgumentNullException("defaultCulture");
- if (collection == null)
- throw new ArgumentNullException("collection");
- collection.Add("Default", defaultCulture);
- Numeric.Initialize(emptyString, collection);
- Date.Initialize(emptyString, collection);
- }
- }
- public abstract class TypeFormat
- {
- public String EmptyString { get; set; }
- public CultureInfo DefaultCulture { get; set; }
- internal abstract void Initialize(
- String emptyString,
- CultureInfoCollection collection);
- }
- public class NumericFormat
- : TypeFormat
- {
- #region Singleton
- private static readonly NumericFormat instance = new NumericFormat();
- internal static NumericFormat Instance { get { return instance; } }
- static NumericFormat() { }
- private NumericFormat() { }
- #endregion
- #region Initialization
- internal override void Initialize(
- String emptyString,
- CultureInfoCollection collection)
- {
- EmptyString = emptyString;
- Collection = collection;
- }
- #endregion
- #region Properties
- private CultureInfoCollection Collection { get; set; }
- #endregion
- #region Public Methods
- public String NumberFormatDecimals(Double numeric, String culture = null)
- {
- return numeric.ToString("N", Collection.GetCulture(culture).NumberFormat);
- }
- public String NumberFormatDecimals(Double? numeric)
- {
- if (numeric.HasValue)
- return NumberFormatDecimals(numeric);
- else return EmptyString;
- }
- public String NumberFormat(Double numeric, String culture = null)
- {
- return numeric.ToString("N0", Collection.GetCulture(culture).NumberFormat);
- }
- public String NumberFormat(Double? numeric)
- {
- if (numeric.HasValue)
- return NumberFormat(numeric);
- else return EmptyString;
- }
- public String NumberFormatDecimals(Single numeric, String culture = null)
- {
- return numeric.ToString("N", Collection.GetCulture(culture).NumberFormat);
- }
- public String NumberFormatDecimals(Single? numeric)
- {
- if (numeric.HasValue)
- return NumberFormatDecimals(numeric);
- else return EmptyString;
- }
- public String NumberFormat(Single numeric, String culture = null)
- {
- return numeric.ToString("N0", Collection.GetCulture(culture).NumberFormat);
- }
- public String NumberFormat(Single? numeric)
- {
- if (numeric.HasValue)
- return NumberFormat(numeric);
- else return EmptyString;
- }
- public String NumberFormat(Int16 numeric, String culture = null)
- {
- return numeric.ToString("N0", Collection.GetCulture(culture).NumberFormat);
- }
- public String NumberFormat(Int16? numeric)
- {
- if (numeric.HasValue)
- return NumberFormat(numeric);
- else return EmptyString;
- }
- public String NumberFormat(Int32 numeric, String culture = null)
- {
- return numeric.ToString("N0", Collection.GetCulture(culture).NumberFormat);
- }
- public String NumberFormat(Int32? numeric)
- {
- if (numeric.HasValue)
- return NumberFormat(numeric);
- else return EmptyString;
- }
- public String NumberFormat(Int64 numeric, String culture = null)
- {
- return numeric.ToString("N0", Collection.GetCulture(culture).NumberFormat);
- }
- public String NumberFormat(Int64? numeric)
- {
- if (numeric.HasValue)
- return NumberFormat(numeric);
- else return EmptyString;
- }
- public String PercentFormatDecimals(Double percent, String culture = null)
- {
- return percent.ToString("P", Collection.GetCulture(culture).NumberFormat);
- }
- public String PercentFormatDecimals(Double? percent)
- {
- if (percent.HasValue)
- return PercentFormatDecimals(percent);
- else return EmptyString;
- }
- public String PercentFormat(Double percent, String culture = null)
- {
- return percent.ToString("P0", Collection.GetCulture(culture).NumberFormat);
- }
- public String PercentFormat(Double? percent)
- {
- if (percent.HasValue)
- return PercentFormat(percent);
- else return EmptyString;
- }
- public String CurrencyFormat(Double currency, String culture = null)
- {
- return currency.ToString("C0", Collection.GetCulture(culture).NumberFormat);
- }
- public String CurrencyFormat(Double? currency)
- {
- if (currency.HasValue)
- return CurrencyFormat(currency.Value);
- else return EmptyString;
- }
- public String CurrencyFormatDecimals(Double currency, String culture = null)
- {
- return currency.ToString("C", Collection.GetCulture(culture).NumberFormat);
- }
- public String CurrencyFormatDecimals(Double? currency)
- {
- if (currency.HasValue)
- return CurrencyFormatDecimals(currency.Value);
- else return EmptyString;
- }
- public String CurrencyFormat(Int32 currency, String culture = null)
- {
- return currency.ToString("C0", Collection.GetCulture(culture).NumberFormat);
- }
- public String CurrencyFormat(Int32? currency)
- {
- if (currency.HasValue)
- return CurrencyFormat(currency.Value);
- else return EmptyString;
- }
- public String CurrencyFormat(Int64 currency, String culture = null)
- {
- return currency.ToString("C0", Collection.GetCulture(culture).NumberFormat);
- }
- public String CurrencyFormat(Int64? currency)
- {
- if (currency.HasValue)
- return CurrencyFormat(currency.Value);
- else return EmptyString;
- }
- public String CurrencyFormat(Single currency, String culture = null)
- {
- return currency.ToString("C0", Collection.GetCulture(culture).NumberFormat);
- }
- public String CurrencyFormat(Single? currency)
- {
- if (currency.HasValue)
- return CurrencyFormat(currency.Value);
- else return EmptyString;
- }
- public String CurrencyFormatDecimals(Single currency, String culture = null)
- {
- return currency.ToString("C", Collection.GetCulture(culture).NumberFormat);
- }
- public String CurrencyFormatDecimals(Single? currency)
- {
- if (currency.HasValue)
- return CurrencyFormatDecimals(currency.Value);
- else return EmptyString;
- }
- #endregion
- }
- public class DateFormat
- : TypeFormat
- {
- #region Singleton
- private static readonly DateFormat instance = new DateFormat();
- internal static DateFormat Instance { get { return instance; } }
- static DateFormat() { }
- private DateFormat() { }
- #endregion
- #region Initialization
- internal override void Initialize(
- String emptyString,
- CultureInfoCollection collection)
- {
- EmptyString = emptyString;
- Collection = collection;
- }
- #endregion
- #region Properties
- private CultureInfoCollection Collection { get; set; }
- #endregion
- #region Public Methods
- public String ShortDate(DateTime date, String culture = null)
- {
- return date.ToString("d", Collection.GetCulture(culture).DateTimeFormat);
- }
- public String ShortDate(DateTime? date)
- {
- if (date.HasValue)
- return LongDate(date.Value);
- else return EmptyString;
- }
- public String LongDate(DateTime date, String culture = null)
- {
- return date.ToString("D", Collection.GetCulture(culture).DateTimeFormat);
- }
- public String LongDate(DateTime? date)
- {
- if (date.HasValue)
- return LongDate(date.Value);
- else return EmptyString;
- }
- public String ShortTime(DateTime date, String culture = null)
- {
- return date.ToString("t", Collection.GetCulture(culture).DateTimeFormat);
- }
- public String ShortTime(DateTime? date)
- {
- if (date.HasValue)
- return ShortTime(date.Value);
- else return EmptyString;
- }
- public String LongTime(DateTime date, String culture = null)
- {
- return date.ToString("T", Collection.GetCulture(culture).DateTimeFormat);
- }
- public String LongTime(DateTime? date)
- {
- if (date.HasValue)
- return LongTime(date.Value);
- else return EmptyString;
- }
- public String FullDateTime(DateTime date, String culture = null)
- {
- return date.ToString("F", Collection.GetCulture(culture).DateTimeFormat);
- }
- public String FullDateTime(DateTime? date)
- {
- if (date.HasValue)
- return FullDateTime(date.Value);
- else return EmptyString;
- }
- #endregion
- }
- public class CultureInfoCollection
- : Dictionary<String, CultureInfo>
- {
- public CultureInfo GetCulture(String name)
- {
- try
- {
- if(String.IsNullOrEmpty(name))
- return this["Default"];
- if (name.Equals("Current", StringComparison.InvariantCultureIgnoreCase))
- return System.Threading.Thread.CurrentThread.CurrentCulture;
- if (ContainsKey(name))
- return this[name];
- else
- {
- CultureInfo wanted = new CultureInfo(name);
- Add(wanted.Name, wanted);
- return wanted;
- }
- }
- catch
- {
- return this["Default"];
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement