Interaction.Partition(Int64, Int64, Int64, Int64) 方法

定义

返回一个字符串,表示包含数字的计算范围。

public:
 static System::String ^ Partition(long Number, long Start, long Stop, long Interval);
public static string Partition(long Number, long Start, long Stop, long Interval);
static member Partition : int64 * int64 * int64 * int64 -> string
Public Function Partition (Number As Long, Start As Long, Stop As Long, Interval As Long) As String

参数

Number
Int64

必填。 Long。 要在其中一个计算范围中找到的整数。

Start
Int64

必填。 Long。 指示计算范围的开始的整数。 Start 不能小于 0。

Stop
Int64

必填。 Long。 指示计算范围集末尾的整数。 Stop 不能小于或等于 Start

Interval
Int64

必填。 Long。 指示在和Start之间Stop计算的每个范围的大小的整数。 Interval 不能小于 1。

返回

一个字符串,表示包含数字的计算范围。

例外

Start < 0、 Stop<= StartInterval< 1。

示例

以下示例设置从 1950 年到 2049 年的数十年的一系列范围。 它查找相应范围内的值 year ,并返回一个 String 显示该范围的值。 例如,如果 year 值为 1984, Partition 则返回“1980:1989”。

Dim year As Long = 1984
' Assume the value of year is provided by data or by user input.
Dim decade As String
decade = Partition(year, 1950, 2049, 10)
MsgBox("Year " & CStr(year) & " is in decade " & decade & ".")

注解

Partition 函数计算一组数值范围,每个范围都包含指定的 Interval值数。 第一个范围从开始 Start,最后一个范围结束于 Stop。 然后,该 Partition 函数标识包含哪些范围 Number 并返回描述该范围的字符串。 范围在字符串中表示为“lowervalue:uppervalue”,其中范围(lowervalue)的低端由冒号(:))与高端(uppervalue)分隔。

如有必要,该 Partition 函数在 下值上值 之前插入前导空格,以便它们具有与值 (Stop + 1) 的字符串表示形式相同的字符数。 这可确保如果将函数的 Partition 输出与多个值 Number一起使用,则会在任何后续排序操作期间正确处理生成的文本。

下表显示了使用三组和三组StartStopInterval计算的范围的一些示例字符串。 “第一个范围”和“最后一个范围”列显示尽可能低和最高的范围,给定值 StartStop值。 “Before first range”和“After last range”列分别显示返回的值 Number 小于 Start 和大于 Stop的字符串。

Start Stop Interval 第一个范围之前 第一个范围 最后一个范围 最后一个范围之后
0 99 5 " : -1" " 0: 4" " 95: 99" "100: "
20 199 10 " : 19" " 20: 29" "190:199" "200: "
100 1010 20 " : 99" " 100: 119" "1000:1010" "1011: "

在上表中,第三行显示当和Start定义一组不能均匀除以Stop的数字时Interval的结果。 最后一个范围结束, Stop使其只有 11 个数字长,即使 Interval 为 20。

如果Interval为 1,则范围为“:NumberNumber,而不考虑Start参数和Stop参数。 例如,如果 Number 为 267, Stop 则为 1000,值为 Interval 1, Partition 则返回“267:267”。

Partition 在构造数据库查询时非常有用。 可以创建一个 SELECT 查询,该查询显示各种值范围内发生的订单数,例如,发票值从 1 到 1000、1001 到 2000 等。

适用于