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。 整數表示每個範圍在與StartStop之間計算出的大小。 Interval 不能少於1。

傳回

一個代表計算範圍的字串,包含一個數字。

例外狀況

Start < 0、 Stop<= Start、或 Interval< 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該範圍的範圍,並回傳描述該範圍的字串。 該範圍在字串中表示為「lowervalueuppervalue」,其中該範圍的低端(較低值)與高頻(上值)之間以冒號(:))分隔。

必要時,函 Partition 數會在 下值上值 前插入前置空格,使它們的字元數與該值Stop 的字串表示( + 1)相同。 這確保了當你使用多個值的Number函式輸出Partition時,所得文字在後續排序操作中都能被正確處理。

下表展示了使用三組 Start、 、 StopInterval和 計算範圍的一些範例字串。 「第一區間」與「最後區間」欄位顯示在 和 StopStart下可能的最低與最高區間。 「第一個範圍之前」和「最後一個範圍之後」欄位分別顯示當值 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: "

在前表中,第三行顯示當 StartStop 定義一組無法被 整除 Interval的數字時的結果。 最後一個範圍結束於 Stop,使得它只有 11 個數字,儘管 Interval 是 20。

Interval為 1,則範圍為「NumberNumber」,不論參數Stop與參數Start如何。 例如,若 Number 為 267, Stop 為 1000, Interval 為 1,則 Partition 回傳 “267: 267”。

Partition 在建構資料庫查詢時非常有用。 你可以建立一個 SELECT 查詢,顯示不同價值範圍內有多少訂單,例如發票值從 1 到 1000、1001 到 2000 等等。

適用於