Last active
August 29, 2015 14:07
-
-
Save jrgcubano/31e09cf1c1465fec88a6 to your computer and use it in GitHub Desktop.
Format Helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class FormatHelper | |
{ | |
#region Numeric Formats | |
public static string DisplayIntegerFormat = "#0."; | |
public static string EditIntegerFormat = "0:#0."; | |
public static string DisplayIntegerDXFormat = "d"; | |
public static string EditIntegerDXFormat = "d"; | |
public static string DisplayDecimalFormat = "#0.00"; | |
public static string EditDecimalFormat = "0:#0.00##"; | |
public static string DisplayDecimalDXFormat = "########0.00"; // TODO | |
public static string EditDecimalDXFormat = "########0.00##"; // TODO | |
public static string DisplayPercentFormat = @"#0.00\%"; | |
public static string EditPercentFormat = @"0:#0.00"; | |
public static string DisplayPercentDXFormat = @"#0.00%%"; | |
public static string EditPercentDXFormat = "@#0.00"; | |
public static string DisplayMoneyFormat = "#0.00"; | |
public static string EditMoneyFormat = "0:#0.00##"; | |
#region Integer | |
public static string GetDisplayInteger() | |
{ | |
return DisplayIntegerFormat; | |
} | |
public static string GetEditInteger() | |
{ | |
return EditIntegerFormat; | |
} | |
public static string FormatInteger(decimal value) | |
{ | |
return value.ToString(GetDisplayInteger()); | |
} | |
#endregion | |
#region Decimal | |
public static string GetDisplayDecimal() | |
{ | |
//NumberFormatInfo nfi = Thread.CurrentThread.CurrentCulture.NumberFormat; | |
//return String.Format("N{0}", nfi.NumberDecimalDigits); | |
return DisplayDecimalFormat; | |
} | |
public static string GetEditDecimal() | |
{ | |
return EditDecimalFormat; | |
} | |
public static string FormatDecimal(decimal value) | |
{ | |
return value.ToString(GetDisplayDecimal()); | |
} | |
#endregion | |
#region Percent | |
public static string GetDisplayPercent() | |
{ | |
return DisplayPercentFormat; | |
} | |
public static string GetEditPercent() | |
{ | |
return EditPercentFormat; | |
} | |
public static string FormatPercent(decimal value) | |
{ | |
return value.ToString(GetDisplayPercent()); | |
} | |
#endregion | |
#region Money | |
public static string GetDisplayMoney() | |
{ | |
return DisplayMoneyFormat; | |
} | |
public static string GetEditMoney() | |
{ | |
return EditMoneyFormat; | |
} | |
public static string FormatMoney(Price price) | |
{ | |
if (!Object.ReferenceEquals(price, null)) | |
{ | |
return FormatMoney(price.Value, price.Currency.Symbol); | |
} | |
return ""; | |
} | |
public static string FormatMoney(decimal value, string symbol) | |
{ | |
string str = ""; | |
int position = GetSymbolPosition(); | |
string valueStr = value.ToString(GetDisplayMoney()); | |
if (position == 0) | |
{ | |
str = String.Format("{0}{1}", symbol, valueStr); | |
} | |
else | |
{ | |
str = String.Format("{0}{1}", valueStr, symbol); | |
} | |
return str; | |
} | |
public static string GetDisplayMoneyWithSymbol(string symbol) | |
{ | |
string pattern = GetDisplayMoney(); | |
if (!String.IsNullOrEmpty(symbol)) | |
{ | |
int position = GetSymbolPosition(); | |
if (position == 0) | |
{ | |
pattern = @"\" + symbol + pattern; | |
} | |
else | |
{ | |
pattern = pattern + @"\" + symbol; | |
} | |
} | |
return pattern; | |
} | |
/// <summary> | |
/// Get Currecy Symbol Position | |
/// 0 = Left, 1 = Right | |
/// </summary> | |
/// <returns></returns> | |
static int _position = -1; | |
public static int GetSymbolPosition() | |
{ | |
if (_position == -1) | |
{ | |
NumberFormatInfo nfi = Thread.CurrentThread.CurrentCulture.NumberFormat; | |
int positivePattern = nfi.CurrencyPositivePattern; | |
if (positivePattern == 0 || positivePattern == 2) | |
{ | |
_position = 0; | |
} | |
else if (positivePattern == 1 || positivePattern == 3) | |
{ | |
_position = 1; | |
} | |
} | |
return _position; | |
} | |
public static string GetNumberSeparator() | |
{ | |
NumberFormatInfo nfi = Thread.CurrentThread.CurrentCulture.NumberFormat; | |
return nfi.CurrencyDecimalSeparator; | |
} | |
#endregion | |
#endregion | |
#region Date Formats (standard) | |
public static string ShortTimePattern = "t"; // h:mm tt | |
public static string ShortDatePattern = "d"; // M/d/yyyy | |
public static string LongTimePattern = "T"; // h:mm:ss tt | |
public static string LongDatePattern = "D"; // dddd, MMMM dd, yyyy | |
public static string LongDateWithShortTimePattern = "f"; // dddd, MMMM dd, yyyy h:mm tt | |
public static string FullDateTimePattern = "F"; // dddd, MMMM dd, yyyy h:mm:ss tt | |
public static string ShortDateWithShortTimePattern = "g"; // M/d/yyyy h:mm tt | |
public static string ShortDateWithShortTimeToFilePattern = "M-d-yyyy_h-mm-tt"; | |
public static string MonthDayPattern = "m"; // MMMM dd | |
public static string YearMonthPattern = "y"; // MMMM, yyyy | |
public static string SortableDateTimePattern = "s"; // yyyy'-'MM'-'dd'T'HH':'mm':'ss (culture independent) | |
public static string UniversalSortableDateTimePattern = "u"; // yyyy'-'MM'-'dd HH':'mm':'ss'Z' (culture independent) | |
public static string FormatShortDate(DateTime dt) | |
{ | |
return dt.ToString(ShortDatePattern); | |
} | |
public static string FormatShortTime(DateTime dt) | |
{ | |
return dt.ToString(ShortTimePattern); | |
} | |
public static string FormatShortDateWithShortTime(DateTime dt) | |
{ | |
return dt.ToString(ShortDateWithShortTimePattern); | |
} | |
public static string FormatShortDateWithShortTimeToFile(DateTime dt) | |
{ | |
return dt.ToString(ShortDateWithShortTimeToFilePattern); | |
} | |
public static string FormatShortTimePoint(TimePoint point) | |
{ | |
try | |
{ | |
DateTime dt = TimePointHelper.Convert(point); | |
return FormatShortDate(dt); | |
} | |
catch | |
{ | |
return ""; | |
} | |
} | |
public static string FormatShortTimePointTime(TimePoint point) | |
{ | |
try | |
{ | |
DateTime dt = TimePointHelper.Convert(point); | |
return FormatShortTime(dt); | |
} | |
catch | |
{ | |
return ""; | |
} | |
} | |
public static string FormatShortTimePointWithShortTime(TimePoint point) | |
{ | |
try | |
{ | |
DateTime dt = TimePointHelper.Convert(point); | |
return FormatShortDateWithShortTime(dt); | |
} | |
catch | |
{ | |
return ""; | |
} | |
} | |
#endregion | |
#region Logics | |
private static string _trueString, _falseString; | |
public static string FormatBoolean(bool value) | |
{ | |
if(value) | |
{ | |
if(String.IsNullOrEmpty(_trueString)) | |
_trueString = Res.Get("LabelTrueString"); | |
return _trueString; | |
} | |
else | |
{ | |
if (String.IsNullOrEmpty(_falseString)) | |
_falseString = Res.Get("LabelFalseString"); | |
return _falseString; | |
} | |
} | |
#endregion | |
/// <summary> | |
/// Remove escape characters in a string sequence | |
/// </summary> | |
/// <param name="src"></param> | |
/// <returns></returns> | |
public static string UnescapeCodes(string src) | |
{ | |
// var rx = new Regex("\\\\([0-9A-Fa-f]+)"); | |
var rx = new Regex("[;\\\\/:*?\"<>|&']"); | |
return rx.Replace(src, ""); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment