Int64 创建数字范围
Int64 creating a number range
我需要能够创建长度超过 19 位的数字范围。
我试过使用
Enumerable.Range(120000003463014,50000).ToList();
这适用于较小的数字,但使用上面的方法时我收到一条错误消息,说它对于 int32 数字来说太大了。有什么方法可以创建一个大数字的连续范围(15 位数字,有时我什至会使用 25 位数字)。提前谢谢你
P.S。我当前问题的起始编号是
128854323463014
结尾 #
128854323513013
您可以创建自己的接受 long
的版本:
public IEnumerable<long> CreateRange(long start, long count)
{
var limit = start + count;
while (start < limit)
{
yield return start;
start++;
}
}
用法:
var range = CreateRange(120000003463014, 50000);
我喜欢使用的一些长扩展:
// ***
// *** Long Extensions
// ***
public static IEnumerable<long> Range(this long start, long count) => start.RangeBy(count, 1);
public static IEnumerable<long> RangeBy(this long start, long count, long by) {
for (; count-- > 0; start += by)
yield return start;
}
public static IEnumerable<long> To(this long start, long finish) => start.ToBy(finish, 1);
public static IEnumerable<long> ToBy(this long start, long end, long by) {
var absBy = Math.Abs(by);
if (start <= end)
for (; start <= end; start += by)
yield return start;
else
for (; start >= end; start -= by)
yield return start;
}
public static IEnumerable<long> DownTo(this long start, long finish) => start.ToBy(finish, 1);
public static IEnumerable<long> DownToBy(this long start, long min, long by) => start.ToBy(min, by);
我需要能够创建长度超过 19 位的数字范围。
我试过使用 Enumerable.Range(120000003463014,50000).ToList();
这适用于较小的数字,但使用上面的方法时我收到一条错误消息,说它对于 int32 数字来说太大了。有什么方法可以创建一个大数字的连续范围(15 位数字,有时我什至会使用 25 位数字)。提前谢谢你
P.S。我当前问题的起始编号是 128854323463014 结尾 # 128854323513013
您可以创建自己的接受 long
的版本:
public IEnumerable<long> CreateRange(long start, long count)
{
var limit = start + count;
while (start < limit)
{
yield return start;
start++;
}
}
用法:
var range = CreateRange(120000003463014, 50000);
我喜欢使用的一些长扩展:
// ***
// *** Long Extensions
// ***
public static IEnumerable<long> Range(this long start, long count) => start.RangeBy(count, 1);
public static IEnumerable<long> RangeBy(this long start, long count, long by) {
for (; count-- > 0; start += by)
yield return start;
}
public static IEnumerable<long> To(this long start, long finish) => start.ToBy(finish, 1);
public static IEnumerable<long> ToBy(this long start, long end, long by) {
var absBy = Math.Abs(by);
if (start <= end)
for (; start <= end; start += by)
yield return start;
else
for (; start >= end; start -= by)
yield return start;
}
public static IEnumerable<long> DownTo(this long start, long finish) => start.ToBy(finish, 1);
public static IEnumerable<long> DownToBy(this long start, long min, long by) => start.ToBy(min, by);