NumberFormater.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Numerics;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. namespace Masuit.Tools.Strings
  9. {
  10. /// <summary>
  11. /// 数制格式化器
  12. /// </summary>
  13. public class NumberFormater
  14. {
  15. /// <summary>
  16. /// 数制表示字符集
  17. /// </summary>
  18. private string Characters { get; }
  19. /// <summary>
  20. /// 进制长度
  21. /// </summary>
  22. public int Length => Characters.Length;
  23. /// <summary>
  24. /// 起始值偏移
  25. /// </summary>
  26. private readonly byte _offset;
  27. /// <summary>
  28. /// 数制格式化器
  29. /// </summary>
  30. public NumberFormater()
  31. {
  32. Characters = "0123456789";
  33. }
  34. /// <summary>
  35. /// 数制格式化器
  36. /// </summary>
  37. /// <param name="characters">符号集</param>
  38. /// <param name="offset">起始值偏移</param>
  39. public NumberFormater(string characters, byte offset = 0)
  40. {
  41. if (string.IsNullOrEmpty(characters))
  42. {
  43. throw new ArgumentException("符号集不能为空");
  44. }
  45. Characters = characters;
  46. _offset = offset;
  47. }
  48. #if NET5_0_OR_GREATER
  49. /// <summary>
  50. /// 数制格式化器
  51. /// </summary>
  52. /// <param name="characters">符号集</param>
  53. /// <param name="offset">起始值偏移</param>
  54. public NumberFormater(ReadOnlySpan<byte> characters, byte offset = 0)
  55. {
  56. if (characters == null || characters.Length == 0)
  57. {
  58. throw new ArgumentException("符号集不能为空");
  59. }
  60. Characters = Encoding.UTF8.GetString(characters);
  61. _offset = offset;
  62. }
  63. /// <summary>
  64. /// 数制格式化器
  65. /// </summary>
  66. /// <param name="characters">符号集</param>
  67. /// <param name="offset">起始值偏移</param>
  68. public NumberFormater(ReadOnlySpan<char> characters, byte offset = 0)
  69. {
  70. if (characters == null || characters.Length == 0)
  71. {
  72. throw new ArgumentException("符号集不能为空");
  73. }
  74. Characters = new string(characters);
  75. _offset = offset;
  76. }
  77. #endif
  78. /// <summary>
  79. /// 数制格式化器
  80. /// </summary>
  81. /// <param name="characters">符号集</param>
  82. /// <param name="offset">起始值偏移</param>
  83. public NumberFormater(byte[] characters, byte offset = 0)
  84. {
  85. if (characters == null || characters.Length == 0)
  86. {
  87. throw new ArgumentException("符号集不能为空");
  88. }
  89. Characters = Encoding.UTF8.GetString(characters);
  90. _offset = offset;
  91. }
  92. /// <summary>
  93. /// 数制格式化器
  94. /// </summary>
  95. /// <param name="characters">符号集</param>
  96. /// <param name="offset">起始值偏移</param>
  97. public NumberFormater(char[] characters, byte offset = 0)
  98. {
  99. if (characters == null || characters.Length == 0)
  100. {
  101. throw new ArgumentException("符号集不能为空");
  102. }
  103. Characters = new string(characters);
  104. _offset = offset;
  105. }
  106. /// <summary>
  107. /// 数制格式化器
  108. /// </summary>
  109. /// <param name="base">进制</param>
  110. /// <param name="offset">起始值偏移</param>
  111. public NumberFormater(byte @base, byte offset = 0)
  112. {
  113. Characters = @base switch
  114. {
  115. <= 2 => "01",
  116. > 2 and < 65 => "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/".Substring(0, @base),
  117. >= 65 and <= 94 => "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!-._\"#$%&'()*+,/:;<=>?@[\\]^`{|}~".Substring(0, @base),
  118. _ => throw new ArgumentException("默认进制最大支持94进制")
  119. };
  120. if (offset >= @base)
  121. {
  122. throw new ArgumentException("偏移量不能超过进制基数" + @base);
  123. }
  124. _offset = offset;
  125. }
  126. /// <summary>
  127. /// 数字转换为指定的进制形式字符串
  128. /// </summary>
  129. /// <param name="number"></param>
  130. public string ToString(long number)
  131. {
  132. if (number == 0)
  133. {
  134. return "0";
  135. }
  136. int start = 0;
  137. int resultOffset = 0;
  138. if (_offset > 0)
  139. {
  140. start = 1;
  141. resultOffset = _offset - 1;
  142. }
  143. number -= resultOffset;
  144. List<char> result = new List<char>();
  145. long t = Math.Abs(number);
  146. while (t != 0)
  147. {
  148. var mod = t % Length;
  149. t = Math.Abs(t / Length);
  150. var character = Characters[Convert.ToInt32(mod) - start];
  151. result.Insert(0, character);
  152. }
  153. if (number < 0)
  154. {
  155. result.Insert(0, '-');
  156. }
  157. return new string(result.ToArray());
  158. }
  159. /// <summary>
  160. /// 数字转换为指定的进制形式字符串
  161. /// </summary>
  162. /// <param name="number"></param>
  163. /// <returns></returns>
  164. public string ToString(BigInteger number)
  165. {
  166. if (number.IsZero)
  167. {
  168. return "0";
  169. }
  170. int start = 0;
  171. int resultOffset = 0;
  172. if (_offset > 0)
  173. {
  174. start = 1;
  175. resultOffset = _offset - 1;
  176. }
  177. number -= resultOffset;
  178. List<char> result = [];
  179. if (number < 0)
  180. {
  181. number = -number;
  182. result.Add('-');
  183. }
  184. var t = number;
  185. while (t != 0)
  186. {
  187. var mod = t % Length;
  188. t = BigInteger.Abs(BigInteger.Divide(t, Length));
  189. var character = Characters[(int)mod - start];
  190. result.Insert(0, character);
  191. }
  192. return new string(result.ToArray());
  193. }
  194. /// <summary>
  195. /// 指定字符串转换为指定进制的数字形式
  196. /// </summary>
  197. /// <param name="str"></param>
  198. /// <returns></returns>
  199. public long FromString(string str)
  200. {
  201. byte start = 0;
  202. int resultOffset = 0;
  203. if (_offset > 0)
  204. {
  205. start = 1;
  206. resultOffset = _offset - 1;
  207. }
  208. int j = 0;
  209. var chars = str.ToCharArray();
  210. Array.Reverse(chars);
  211. return chars.Where(Characters.Contains).Sum(ch => (Characters.IndexOf(ch) + start) * (long)Math.Pow(Length, j++)) + resultOffset;
  212. }
  213. /// <summary>
  214. /// 指定字符串转换为指定进制的大数形式
  215. /// </summary>
  216. /// <param name="str"></param>
  217. /// <returns></returns>
  218. public BigInteger FromStringBig(string str)
  219. {
  220. byte start = 0;
  221. int resultOffset = 0;
  222. if (_offset > 0)
  223. {
  224. start = 1;
  225. resultOffset = _offset - 1;
  226. }
  227. int j = 0;
  228. var charArray = str.ToCharArray();
  229. Array.Reverse(charArray);
  230. var chars = charArray.Where(Characters.Contains);
  231. return chars.Aggregate(BigInteger.Zero, (current, c) => current + (Characters.IndexOf(c) + start) * BigInteger.Pow(Length, j++)) + resultOffset;
  232. }
  233. /// <summary>Returns a string that represents the current object.</summary>
  234. /// <returns>A string that represents the current object.</returns>
  235. public override string ToString()
  236. {
  237. return Length + "进制模式,进制符:" + Characters;
  238. }
  239. // 转换数字
  240. private static char ToNum(char x)
  241. {
  242. const string strChnNames = "零一二三四五六七八九";
  243. const string strNumNames = "0123456789";
  244. return strChnNames[strNumNames.IndexOf(x)];
  245. }
  246. // 转换万以下整数
  247. private static string ChangeInt(string x)
  248. {
  249. string[] strArrayLevelNames =
  250. {
  251. "",
  252. "十",
  253. "百",
  254. "千"
  255. };
  256. string ret = "";
  257. int i;
  258. for (i = x.Length - 1; i >= 0; i--)
  259. {
  260. if (x[i] == '0')
  261. {
  262. ret = ToNum(x[i]) + ret;
  263. }
  264. else
  265. {
  266. ret = ToNum(x[i]) + strArrayLevelNames[x.Length - 1 - i] + ret;
  267. }
  268. }
  269. while ((i = ret.IndexOf("零零", StringComparison.Ordinal)) != -1)
  270. {
  271. ret = ret.Remove(i, 1);
  272. }
  273. if (ret[ret.Length - 1] == '零' && ret.Length > 1)
  274. {
  275. ret = ret.Remove(ret.Length - 1, 1);
  276. }
  277. if (ret.Length >= 2 && ret.Substring(0, 2) == "一十")
  278. {
  279. ret = ret.Remove(0, 1);
  280. }
  281. return ret;
  282. }
  283. // 转换整数
  284. private static string ToInt(string x)
  285. {
  286. int len = x.Length;
  287. string result;
  288. string temp;
  289. if (len <= 4)
  290. {
  291. result = ChangeInt(x);
  292. }
  293. else if (len <= 8)
  294. {
  295. result = ChangeInt(x.Substring(0, len - 4)) + "万";
  296. temp = ChangeInt(x.Substring(len - 4, 4));
  297. if (temp.IndexOf("千", StringComparison.Ordinal) == -1 && !string.IsNullOrEmpty(temp))
  298. {
  299. result += "零" + temp;
  300. }
  301. else
  302. {
  303. result += temp;
  304. }
  305. }
  306. else
  307. {
  308. result = ChangeInt(x.Substring(0, len - 8)) + "亿";
  309. temp = ChangeInt(x.Substring(len - 8, 4));
  310. if (temp.IndexOf("千", StringComparison.Ordinal) == -1 && !string.IsNullOrEmpty(temp))
  311. {
  312. result += "零" + temp;
  313. }
  314. else
  315. {
  316. result += temp;
  317. }
  318. result += "万";
  319. temp = ChangeInt(x.Substring(len - 4, 4));
  320. if (temp.IndexOf("千", StringComparison.Ordinal) == -1 && !string.IsNullOrEmpty(temp))
  321. {
  322. result += "零" + temp;
  323. }
  324. else
  325. {
  326. result += temp;
  327. }
  328. }
  329. int i;
  330. if ((i = result.IndexOf("零万", StringComparison.Ordinal)) != -1)
  331. {
  332. result = result.Remove(i + 1, 1);
  333. }
  334. while ((i = result.IndexOf("零零", StringComparison.Ordinal)) != -1)
  335. {
  336. result = result.Remove(i, 1);
  337. }
  338. if (result[result.Length - 1] == '零' && result.Length > 1)
  339. {
  340. result = result.Remove(result.Length - 1, 1);
  341. }
  342. return result;
  343. }
  344. /// <summary>
  345. /// 转换为中文数字格式
  346. /// </summary>
  347. /// <param name="num">123.45</param>
  348. /// <returns></returns>
  349. public static string ToChineseNumber(IConvertible num)
  350. {
  351. var x = num.ToString(CultureInfo.CurrentCulture);
  352. if (x.Length == 0)
  353. {
  354. return "";
  355. }
  356. string result = "";
  357. if (x[0] == '-')
  358. {
  359. result = "负";
  360. x = x.Remove(0, 1);
  361. }
  362. if (x[0].ToString() == ".")
  363. {
  364. x = "0" + x;
  365. }
  366. if (x[x.Length - 1].ToString() == ".")
  367. {
  368. x = x.Remove(x.Length - 1, 1);
  369. }
  370. if (x.IndexOf(".") > -1)
  371. {
  372. result += ToInt(x.Substring(0, x.IndexOf("."))) + "点" + x.Substring(x.IndexOf(".") + 1).Aggregate("", (current, t) => current + ToNum(t));
  373. }
  374. else
  375. {
  376. result += ToInt(x);
  377. }
  378. return result;
  379. }
  380. /// <summary>
  381. /// 数字转中文金额大写
  382. /// </summary>
  383. /// <param name="number">22.22</param>
  384. public static string ToChineseMoney(IConvertible number)
  385. {
  386. var m = number.ConvertTo<decimal>();
  387. if (m == 0)
  388. {
  389. return "零元整";
  390. }
  391. /*
  392. #:用数字替换字符位置,如果数字小于对应值的位数,则在左侧填充零。
  393. L:将整数转换为一个字符串,并将其转换为小写字母形式。
  394. E:将数字格式化为科学计数法,并使用大写字母 E 表示指数。
  395. D:将数字格式化为整数,并使用逗号分隔数字组。
  396. C:将数字转换为货币格式,并使用本地货币符号。
  397. K:将数字格式化为千位分隔数字,使用 K 表示千。
  398. J:将数字格式化为十位分隔数字,使用 J 表示十。
  399. I:将数字格式化为百位分隔数字,使用 I 表示百。
  400. H:将数字格式化为千万位分隔数字,使用 H 表示千万。
  401. G:将数字格式化为一般格式,根据数字的大小和精度选择固定点或科学计数法表示。
  402. F:将数字格式化为固定点格式,并指定小数位数。
  403. .0:指定小数点后的位数为零。
  404. B:将数字转换为二进制格式。
  405. A:将数字转换为 ASCII 字符。
  406. */
  407. var s = m.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");
  408. /*
  409. * ((?<=-|^)[^1-9]*): 匹配负号(如果存在),并且匹配在小数点前面的所有非数字字符。
  410. * ((?'z'0)[0A-E]*((?=[1-9])|(?'-z'(?=[F-L\.]|$)))): 匹配小数点前面的数字。首先匹配一个零,然后匹配任意数量的零到 E 的字母。接下来,它匹配非零数字,或者如果遇到了小数点、字母 F-L 或者字符串的结尾,它会匹配上一个“-z”(即前面匹配的零)。
  411. * ((?'b'[F-L])(?'z'0)[0A-L]*((?=[1-9])|(?'-z'(?=[\  .]|$)))): 匹配小数点后面的数字。首先匹配字母 F-L,并将其存储在组“b”中。接着,它匹配一个零,并将其存储在组“z”中。然后,它匹配任意数量的零到字母 L 的字母。最后,匹配非零数字,或者如果遇到了小数点或字符串的结尾,它会匹配上一个“-z”(即前面匹配的零)。
  412. */
  413. var 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}");
  414. /*
  415. * 将其转换为对应的中文大写字符,例如将'1'转换为'壹',将'2'转换为'贰',以此类推。Lambda表达式中使用了一个映射表,通过字符的ASCII码值来查找对应的中文字符。
  416. */
  417. return Regex.Replace(d, ".", t => "负元空零壹贰叁肆伍陆柒捌玖空空空空空空空分角拾佰仟萬億兆京垓秭穰"[t.Value[0] - '-'].ToString()).Next(x => m < 0 ? "负" + x : x);
  418. }
  419. }
  420. }