Unity3D--⼤额数值单位转换
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");
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)writeline输出数值变量
{
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");
textTemp = coin.ToString("0");            }
return textTemp;
}
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。