20位长度的数字比较大小

0

parseInt取整，长度超过18位，末尾都成0了，所以，当长度为20位时，实行前10位进行切割，分别比较。

0

JS的情况：

12345678912345678912 < 12345678912345678910
false
12345678912345678912 > 12345678912345678910
false

1234567891234568 < 1234567891234560
false
1234567891234568 > 1234567891234560
true

junio.cn | 菜鸟二级 |园豆：445 | 2017-02-19 23:07

0

0

Boblim | 园豆：492 (菜鸟二级) | 2017-02-13 09:38
0

js-BIO 是一个在 JavaScript 中对大整数进行常用算术运算的类库。比如可以相减看是否大于零，点击测试

0

Long，注意是大写的封装类

CaiYongji | 园豆：1267 (小虾三级) | 2017-02-13 10:42
0

"12345678901234567890".length

20

parseInt("12345678901234567");
12345678901234568
parseInt("1234567890123456");
1234567890123456

0

BigDecimal  用这个

http://zhangyinhu8680.iteye.com/blog/1536397

_zpq | 园豆：204 (菜鸟二级) | 2017-02-13 14:40
0

/// <summary>
/// 将字符串数字乘一个小于10的数字
/// </summary>
/// <param name="num">小于</param>
/// <param name="str"></param>
/// <returns></returns>
string strCF(byte num, string str)
{
if (num > 9) throw new Exception("请不要传递大于9的数字！");
else if (num == 0) return "0";
else if (num == 1) return str;
List<char> result = new List<char>();
str = string.Join("", str.Reverse());
for (int i = 0; i < str.Length; i++)
{
byte b = Convert.ToByte(str[i].ToString());
b *= num;
if (result.Count - 1 == i)
{
b += Convert.ToByte(result[i].ToString());//加上原来数据
result.RemoveAt(i);
}
}
result.Reverse();
if (result.All(c => c.Equals('0'))) result = new List<char>() { '0' };
return string.Join("", result);
}
/// <summary>
/// 字符串加法
/// </summary>
/// <param name="num1"></param>
/// <param name="num2"></param>
/// <returns></returns>
{
List<char> result = new List<char>();
num1 = string.Join("", num1.Reverse());
num2 = string.Join("", num2.Reverse());
int len = num1.Length < num2.Length ? num1.Length : num2.Length;
for (int i = 0; i < len; i++)
{
byte b = Convert.ToByte(int.Parse(num1[i].ToString()) + int.Parse(num2[i].ToString()));
if (result.Count - 1 == i)
{
b += Convert.ToByte(result[i].ToString());//加上原来数据
result.RemoveAt(i);
}
}
if (num1.Length != num2.Length)
{
string last = (num1.Length > num2.Length ? num1 : num2).Substring(len);
if (result.Count != len)
{//不管怎么加最多只可能进1
int lastNum = int.Parse(last[0].ToString()) + int.Parse(result.Last().ToString());
if (lastNum < 10)
last = lastNum + last.Substring(1);
else
last = string.Join("", strAdd(string.Join("", last.Reverse()), result.Last().ToString()).Reverse());

result.RemoveAt(result.Count - 1);
}
}
result.Reverse();
if (result.All(c => c.Equals('0'))) result = new List<char>() { '0'};
return string.Join("", result);
}
/// <summary>
/// 字符串减法
/// </summary>
/// <param name="num1"></param>
/// <param name="num2"></param>
/// <returns></returns>
string strSubtract(string num1, string num2)
{
List<char> result = new List<char>();
if (num1.Length >= num2.Length)
{
num1 = string.Join("", num1.Reverse());
num2 = string.Join("", num2.Reverse());
int len = num1.Length;
for (int i = 0; i < len; i++)
{
short b = Convert.ToInt16(int.Parse((num1.Length > i ? num1[i] : '0').ToString()) - int.Parse((num2.Length > i ? num2[i] : '0').ToString()));
if (b < 0)
{
if (i == len - 1) //最后一次还需要借位的话
{
num1 = Math.Abs(b).ToString();
for (int j = 0; j < result.Count; j++)
num1 = num1 + "0";
return "-" + strSubtract(num1, string.Join("", result));
}
b += 10;
num2 = num2.Substring(0, i + 1) + string.Join("", strAdd(string.Join("", num2.Substring(i + 1).Reverse()), "1").Reverse());
len = num1.Length > num2.Length ? num1.Length : num2.Length;
}
}
}
else
//if (num1.Length < num2.Length)
return "-" + strSubtract(num2, num1);
//else
//{ //位数相等
// int i = 0, b;
// do
// {
// b = int.Parse((num1.Length > i ? num1[i] : '0').ToString()) - int.Parse((num2.Length > i ? num2[i] : '0').ToString());
// if (b > 0) return strSubtract("0" + num1, num2);
// else if (b < 0) return "-" + strSubtract("0" + num2, num1);
// } while (b == 0);
//}
//if (num1.Length != num2.Length)
//{
// string last = (num1.Length > num2.Length ? num1 : num2).Substring(len);
// if (result.Count != len)
// {//不管怎么加最多只可能进1
// //但是49+1之类的情况就会出现问题
// int lastNum = int.Parse(last[0].ToString()) + int.Parse(result.Last().ToString());
// if (lastNum < 10)
// last = lastNum + last.Substring(1);
// else
// last = string.Join("", strAdd(string.Join("", last.Reverse()), result.Last().ToString()).Reverse());

// result.RemoveAt(result.Count - 1);
// }
//}
result.Reverse();
if (result.All(c => c.Equals('0'))) result = new List<char>() { '0' };
return string.Join("", result);
}
/// <summary>
/// 字符串乘法
/// </summary>
/// <param name="num1"></param>
/// <param name="num2"></param>
/// <returns></returns>
string strCF(string num1, string num2)
{
string result = "0";
if (num1.Length <= num2.Length)
{
num1 = string.Join("", num1.Reverse());
foreach (char item in num1)//优化算法逻辑 把乘数的一端的0移到另一端
{
if (item != '0') break;
else
{
num1 = num1.Substring(1);
num2 = num2 + "0";
}
}
for (int i = 0; i < num1.Length; i++)
{
string b0 = "";
for (int j = 0; j < i; j++)
b0 += "0";
result = strAdd(result, strCF(Convert.ToByte(num1[i].ToString()), num2 + b0));
}
}
else return strCF(num2, num1);//反过来调用 加快运行速度减少循环次数
return result;
}

0

【秦时明月】 | 园豆：803 (小虾三级) | 2017-02-13 19:25
0

hobotree | 园豆：206 (菜鸟二级) | 2017-02-16 11:32
0

您需要登录以后才能回答，未注册用户请先注册