using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace Bowin.Common.Utility { public static class MathEx { /// /// 取输入值中大于等于0的部分(取正数) /// /// /// public static int UponZero(this int input) { return (input >= 0) ? input : 0; } /// /// 取输入值中大于等于0的部分(取正数) /// /// /// public static int? UponZero(this int? input) { return ((input ?? 0) >= 0) ? input : 0; } /// /// 取输入值中大于等于0的部分(取正数) /// /// /// public static double UponZero(this double input) { return (input >= 0) ? input : 0; } /// /// 取输入值中大于等于0的部分(取正数) /// /// /// public static double? UponZero(this double? input) { return ((input ?? 0) >= 0) ? input : 0; } /// /// 取输入值中大于等于0的部分(取正数) /// /// /// public static decimal UponZero(this decimal input) { return (input >= 0) ? input : 0; } /// /// 取输入值中大于等于0的部分(取正数) /// /// /// public static decimal? UponZero(this decimal? input) { return ((input ?? 0) >= 0) ? input : 0; } /// /// 数字转大写 /// /// /// public static string ConvertToChinese(this double x) { string s = x.ToString("#L#E#D#C#K#E#D#C#J#E#D#C#I#E#D#C#H#E#D#C#G#E#D#C#F#E#D#C#.0B0A"); string d = Regex.Replace(s, @"((?<=-|^)[^1-9]*)|((?'z'0)[0A-E]*((?=[1-9])|(?'-z'(?=[F-L\.]|$))))|((?'b'[F-L])(?'z'0)[0A-L]*((?=[1-9])|(?'-z'(?=[\.]|$))))", "${b}${z}"); return Regex.Replace(d, ".", delegate(Match m) { return "负空空零一二三四五六七八九空空空空空空空空空十百千万亿兆京垓秭穰"[m.Value[0] - '-'].ToString(); }); } /// /// 数字转大写 /// /// /// public static string ConvertToChinese(this int x) { string s = x.ToString("#L#E#D#C#K#E#D#C#J#E#D#C#I#E#D#C#H#E#D#C#G#E#D#C#F#E#D#C#"); string d = Regex.Replace(s, @"((?<=-|^)[^1-9]*)|((?'z'0)[0A-E]*((?=[1-9])|(?'-z'(?=[F-L\.]|$))))|((?'b'[F-L])(?'z'0)[0A-L]*((?=[1-9])|(?'-z'(?=[\.]|$))))", "${b}${z}"); return Regex.Replace(d, ".", delegate(Match m) { return "负空空零一二三四五六七八九空空空空空空空空空十百千万亿兆京垓秭穰"[m.Value[0] - '-'].ToString(); }).Replace("一十", "十"); } /// /// 数字转大写金额 /// /// /// public static string ConvertToChineseCurrency(double x) { string s = x.ToString("#L#E#D#C#K#E#D#C#J#E#D#C#I#E#D#C#H#E#D#C#G#E#D#C#F#E#D#C#.0B0A"); string d = Regex.Replace(s, @"((?<=-|^)[^1-9]*)|((?'z'0)[0A-E]*((?=[1-9])|(?'-z'(?=[F-L\.]|$))))|((?'b'[F-L])(?'z'0)[0A-L]*((?=[1-9])|(?'-z'(?=[\.]|$))))", "${b}${z}"); return Regex.Replace(d, ".", delegate(Match m) { return "负元空零壹贰叁肆伍陆柒捌玖空空空空空空空分角拾佰仟万亿兆京垓秭穰"[m.Value[0] - '-'].ToString(); }); } /// /// 数字转大写金额 /// /// /// public static string ConvertToChineseBig(int x) { if (x > 10 & x < 0) { throw new Exception("请输入个位数。"); } string[] s = new string[] { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" }; return s[x]; } } }