123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- namespace Bowin.Common.Utility
- {
- public static class MathEx
- {
- /// <summary>
- /// 取输入值中大于等于0的部分(取正数)
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- public static int UponZero(this int input)
- {
- return (input >= 0) ? input : 0;
- }
- /// <summary>
- /// 取输入值中大于等于0的部分(取正数)
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- public static int? UponZero(this int? input)
- {
- return ((input ?? 0) >= 0) ? input : 0;
- }
- /// <summary>
- /// 取输入值中大于等于0的部分(取正数)
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- public static double UponZero(this double input)
- {
- return (input >= 0) ? input : 0;
- }
- /// <summary>
- /// 取输入值中大于等于0的部分(取正数)
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- public static double? UponZero(this double? input)
- {
- return ((input ?? 0) >= 0) ? input : 0;
- }
- /// <summary>
- /// 取输入值中大于等于0的部分(取正数)
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- public static decimal UponZero(this decimal input)
- {
- return (input >= 0) ? input : 0;
- }
- /// <summary>
- /// 取输入值中大于等于0的部分(取正数)
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- public static decimal? UponZero(this decimal? input)
- {
- return ((input ?? 0) >= 0) ? input : 0;
- }
- /// <summary>
- /// 数字转大写
- /// </summary>
- /// <param name="x"></param>
- /// <returns></returns>
- 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();
- });
- }
- /// <summary>
- /// 数字转大写
- /// </summary>
- /// <param name="x"></param>
- /// <returns></returns>
- 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("一十", "十");
- }
- /// <summary>
- /// 数字转大写金额
- /// </summary>
- /// <param name="x"></param>
- /// <returns></returns>
- 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();
- });
- }
- /// <summary>
- /// 数字转大写金额
- /// </summary>
- /// <param name="x"></param>
- /// <returns></returns>
- public static string ConvertToChineseBig(int x)
- {
- if (x > 10 & x < 0)
- {
- throw new Exception("请输入个位数。");
- }
- string[] s = new string[] { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
- return s[x];
- }
- }
- }
|