赞
踩
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace Test
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine(Numdispose("45555555207"));
- string[] symbol = {"个", "千", "百万", "十亿", "万亿" };
- Console.WriteLine(NumUnit(45555555207, symbol));
- Console.WriteLine(FormatNum(45555555207, 3));
- Console.ReadKey();
- }
-
- /// <summary>
- /// 数字换算
- /// </summary>
- /// <param name="num"></param>
- /// <returns></returns>
- public static string Numdispose(string num)
- {
- string[] symbol = { "K", "M", "B", "T", "aa", "ab", "ac", "ad" };
-
- string str1 = string.Empty;
-
- string str2 = string.Empty;
-
- if (num.Length > 6)
- {
- int a = (num.Length - 6) / 3;
-
- str1 = num.Substring(0, (num.Length - (3 * (a + 1))));
-
- int b = num.Length - (3 * (a + 1));
-
- str2 = num[b].ToString();
-
- if (int.Parse(str2) >= 5) str1 = (int.Parse(str1) + 1).ToString();
-
- if (str1.Length > 3) return str1.Insert(str1.Length - 3, ",") + symbol[a];
-
- return str1 + symbol[a];
- }
- if (num.Length > 3 && num.Length < 7)
- {
- return num.Insert(num.Length - 3, ",");
- }
- return num;
- }
-
-
- public static string NumUnit(double num, string[] unitArray)
- {
- double tempNum = num;
- if (tempNum < 10000)
- {
- return num.ToString("0");
- }
-
- int unitIndex = 0;
- while (tempNum / 10000 / 100 >= 1)
- {
- unitIndex++;
- if (unitIndex >= unitArray.Length)
- {
- unitIndex = unitArray.Length - 1;
- break;
- }
- tempNum /= 100;
- }
- return (tempNum / 10000).ToString("0.00") + unitArray[unitIndex];
- }
-
- /// <summary>
- /// 数值转换 效果最好
- /// </summary>
- /// <param name="coin">数值</param>
- /// <param name="fixNum">保留小数点数</param>
- /// <returns></returns>
- public static string FormatNum(decimal coin, int fixNum)
- {
- string textTemp = string.Empty;
- string text3 = "f" + fixNum.ToString();
- if (coin >= 100000000000000000000000000m)
- {
- textTemp = (coin / 100000000000000000000000000m).ToString(text3) + "ae";
- }
- else if (coin >= 1000000000000000000000000m)
- {
- textTemp = (coin / 1000000000000000000000000m).ToString(text3) + "ad";
- }
- else if (coin >= 1000000000000000000000m)
- {
- textTemp = (coin / 1000000000000000000000m).ToString(text3) + "ac";
- }
- else if (coin >= new decimal(1000000000000000000L))
- {
- textTemp = (coin / new decimal(1000000000000000000L)).ToString(text3) + "ab";
- }
- else if (coin >= new decimal(1000000000000000L))
- {
- textTemp = (coin / new decimal(1000000000000000L)).ToString(text3) + "aa";
- }
- else if (coin >= new decimal(1000000000000L))
- {
- textTemp = (coin / new decimal(1000000000000L)).ToString(text3) + "t";
- }
- else if (coin >= 1000000000m)
- {
- textTemp = (coin / 1000000000m).ToString(text3) + "b";
- }
- else if (coin >= 1000000m)
- {
- textTemp = (coin / 1000000m).ToString(text3) + "m";
- }
- else if (coin >= 1000m)
- {
- textTemp = (coin / 1000m).ToString(text3) + "k";
- }
- else
- {
- textTemp = coin.ToString("0");
- }
-
- return textTemp;
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。