public static string Format(object value, int decimalDigits, int decimalScale, bool displayThousandSeperator, bool displayEmptyForZero) { string result; if (value == null) { result = string.Empty; } else if (value.GetType() == typeof(string)) { result = (string)value; } else { decimal num = System.Convert.ToDecimal(value); if (num != 0m) { if (decimalDigits > 0) { result = NumberUtils.Format(NumberUtils.Round(num, decimalDigits), "n" + decimalDigits, displayThousandSeperator); } else if (decimalScale > 0) { num = NumberUtils.Round(num, decimalScale); result = (displayThousandSeperator ? NumberUtils.FormatWithThousandSeperator(num, decimalScale) : NumberUtils.Format(num)); } else if (displayThousandSeperator) { result = NumberUtils.FormatWithThousandSeperator(num, 8); } else { result = NumberUtils.Format(num); } } else { result = (displayEmptyForZero ? string.Empty : NumberUtils.Format(num)); } } return(result); }
public static string Format(object value, int decimalDigits, int decimalScale) { return(NumberUtils.Format(value, decimalDigits, decimalScale, false, false)); }
public static string Format(object value) { return(NumberUtils.Format(value, 0, 8)); }
public static string GetUpperCase(object value, int decimalDigits, NumberUpperCaseKind upperCaseKind) { string result; decimal num; if (value == null || value.ToString().Trim() == "" || (value.GetType() == typeof(double) && (double.IsNaN((double)value) || double.IsInfinity((double)value)))) { result = string.Empty; } else if (!decimal.TryParse(value.ToString(), out num)) { result = value.ToString(); } else { if (decimalDigits > 0) { num = NumberUtils.Round(num, decimalDigits); } string str = string.Empty; if (num < 0m) { str = "负"; } string numberBits = NumberUtils.GetNumberBits(System.Math.Abs(num), 0); if (numberBits.Length > 12 || numberBits == "E") { result = "面值太大,不能转换"; } else { string numberBits2 = NumberUtils.GetNumberBits(System.Math.Abs(num), 1); int length = numberBits.Length; string text = string.Empty; string[] array = new string[] { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" }; string[] array2 = new string[] { "", "", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万" }; string[] array3 = new string[] { "圆", "角", "分" }; for (int i = 1; i <= length; i++) { string s = numberBits.Substring(length - i, 1); text = array[int.Parse(s)] + array2[i] + text; } text = text.Replace("拾零", "拾"); text = text.Replace("零拾", "零"); text = text.Replace("零佰", "零"); text = text.Replace("零仟", "零"); text = text.Replace("零万", "零"); for (int i = 1; i <= 6; i++) { text = text.Replace("零零", "零"); } text = text.Replace("零万", "零"); text = text.Replace("零亿", "零"); text = text.Replace("零零", "零"); if (text.Length > 1) { if (text.Substring(text.Length - 1, 1) == "零") { text = text.Remove(text.Length - 1, 1); } } string str2 = ""; if (numberBits2 != "0") { str2 = NumberUtils.GetDecimalUpperDisplay(numberBits2, array, upperCaseKind); } result = str + text + str2; } } return(result); }
internal static void Test() { decimal number = 0.245m; System.Console.WriteLine(NumberUtils.Round(number, 2)); }
private static string FormatWithThousandSeperator(decimal value, int digits) { string str = value.ToString("n" + digits); return(NumberUtils.FormatWithThousandSeperatorExtracted(str)); }