因为游戏中需要用到部分数值过大,超过int,ulong的值范围,所以自己简单的写了个计算大数值的类。
ps:因为该计算次数较少所以简单的写了个静态
public class Digital : MonoBehaviour
{
//加
public static string DigitalCalAdd(string a, string b)
{
a = ChangeToNoDecimal(a);
b = ChangeToNoDecimal(b);
if (a.Length < 11 && b.Length < 11)
{
ulong f = ulong.Parse(a) + ulong.Parse(b);
return f.ToString();
}
else
{
StringBuilder longer;
StringBuilder shorter;
if (b.Length > a.Length)
{
longer = new StringBuilder(b);
shorter = new StringBuilder(a);
}
else
{
longer = new StringBuilder(a);
shorter = new StringBuilder(b);
}
StringBuilder str = new StringBuilder(""); ;
int last = 0;
for (int i = 1; i <= longer.Length; i++)
{
if (i == longer.Length)
{
int cal;
if (longer.Length > shorter.Length)
{
cal = System.Convert.ToInt32(longer[longer.Length - i]) + last - 48;
}
else
{
cal = System.Convert.ToInt32(longer[longer.Length - i]) + System.Convert.ToInt32(shorter[shorter.Length - i]) + last - 96;
}
str = str.Insert(0, (cal).ToString());
last = 0;
}
else if (i <= shorter.Length)
{
int cal = System.Convert.ToInt32(longer[longer.Length - i]) + System.Convert.ToInt32(shorter[shorter.Length - i]) + last - 96;
str = str.Insert(0, (cal % 10).ToString());
last = cal / 10;
}
else
{
int cal = System.Convert.ToInt32(longer[longer.Length - i]) + last - 48;
str = str.Insert(0, (cal % 10).ToString());
last = cal / 10;
}
}
return str.ToString();
}
}
//减
public static string DigitalCalReverse(string a, string b)//整数 不含小数
{
a = ChangeToNoDecimal(a);
b = ChangeToNoDecimal(b);
if (a.Length < 11 && b.Length < 11)
{
ulong f = ulong.Parse(a) - ulong.Parse(b);
return f.ToString();
}
else
{
StringBuilder longer;
StringBuilder shorter;
if (b.Length > a.Length)
{
longer = new StringBuilder(b);
shorter = new StringBuilder(a);
}
else
{
longer = new StringBuilder(a);
shorter = new StringBuilder(b);
}
StringBuilder str = new StringBuilder("");
int last = 0;
for (int i = 1; i <= longer.Length; i++)
{
if (i == longer.Length)
{
int cal;
if (longer.Length > shorter.Length)
{
cal = System.Convert.ToInt32(longer[longer.Length - i]) + last - 48;
}
else
{
cal = System.Convert.ToInt32(longer[longer.Length - i]) - System.Convert.ToInt32(shorter[shorter.Length - i]) + last;
}
str = str.Insert(0, (cal).ToString());
last = 0;
}
else if (i <= shorter.Length)
{
int cal = System.Convert.ToInt32(longer[longer.Length - i]) - System.Convert.ToInt32(shorter[shorter.Length - i]) + last + 10;
str = str.Insert(0, (cal % 10).ToString());
last = (cal / 10) - 1;
}
else
{
int cal = System.Convert.ToInt32(longer[longer.Length - i]) + last - 38;
str = str.Insert(0, (cal % 10).ToString());
last = (cal / 10) - 1;
}
}
return DeleteLeftZero(str.ToString());
;
}
}
//删除掉字符串最左边的0
static string DeleteLeftZero(string s)
{
if (s[0] == ‘0‘)
{
s = s.Remove(0, 1);
return DeleteLeftZero(s);
}
else
{
return s;
}
}
static string ChangeToNoDecimal(string s)
{
if (s.Contains("."))
{
string[] sr = s.Split(‘.‘);
s = sr[0];
}
return s;
}
//乘
public static string DigitalCalMultiple(string a, string b)
{
a = ChangeToNoDecimal(a);
b = ChangeToNoDecimal(b);
if ((a.Length + b.Length) <= 11)
{
ulong f = ulong.Parse(a) * ulong.Parse(b);
return f.ToString();
}
else
{
StringBuilder longer;
StringBuilder shorter;
if (b.Length > a.Length)
{
longer = new StringBuilder(b);
shorter = new StringBuilder(a);
}
else
{
longer = new StringBuilder(a);
shorter = new StringBuilder(b);
}
StringBuilder str = new StringBuilder("");
int last = 0;
for (int i = 1; i <= longer.Length; i++)
{
if (i == longer.Length)
{
int cal;
cal = (System.Convert.ToInt32(longer[longer.Length - i]) - 48) * int.Parse(shorter.ToString()) + last;
str = str.Insert(0, (cal).ToString());
last = 0;
}
else if (i < longer.Length)
{
int cal = (System.Convert.ToInt32(longer[longer.Length - i]) - 48) * int.Parse(shorter.ToString()) + last;
str = str.Insert(0, (cal % 10).ToString());
last = cal / 10;
}
}
return str.ToString();
}
}
string[] unit = { "k", "m", "b", "t" };
int[] unitNum = { 3, 6, 9, 12 };
//变换为显示的值
public static string ToShow(string str)
{
return str;
}
//返回前者比后者大
public static bool Compare(string big, string small)
{
if (big.Length != small.Length)
{
return big.Length > small.Length;
}
else
{
for (int i = 0; i < big.Length; i++)
{
if (big[i] != small[i])
{
return big[i] > small[i];
}
}
}
return true;
}
public static bool IfBiggerZero(string str)
{
if (str[0]!=‘-‘)
{
return true;
}
else
{
return false;
}
}
}
原文:https://www.cnblogs.com/ydq666/p/11027917.html