# VB的計算問題：求商

• ### 問題

• 剛接觸到VB的世界，發現了一個怪異的問題：
C#的程式
int first = 743;
int second = 15;
int result = first / second;
Console.WriteLine(result);
輸出結果是49

同樣的運算式轉到VB.Net
Dim first As Integer = 743
Dim second As Integer = 15
Dim result As Integer = first / second
Console.WriteLine(result)
卻變成50。

如果把first設成742，C#的結果不變，VB.Net的卻變成49。
似乎VB.Net會自動四捨五入成整數？如何避免VB.Net如此智慧化的行為呢？
因為first的數值是一直變動的，照理說如果被除數的增加量(或減少量)小於除數，則商數應該是不會變化的才對(例如C#的結果)，在此被除數只少了1，結果卻由50變成49，實在是造成困擾，因為之後必須拿商數&餘數去定義陣列元素的大小說

2009年4月6日 下午 06:49

### 解答

• 求商可以用 \
743 \ 15 = 49
• 已標示為解答 2009年4月7日 下午 01:50
2009年4月7日 上午 01:32
• ### Integer Division

I'm working on modifying some old code for a report that prints out some address labels.  Language is VB.Net.  The labels are each 1" (or 100 pixels in my case) high and there is a 1/2" (50px) margin at the top.  There is already a variable available that stores the current vertical position of the 'cursor' as I write data to the report.  The piece I'm working on advances the cursor to the top of the next label.  What I want to do is this:
YPos = ((((YPos-50) / 100) + 1) * 100) + 50
I learned to program in C/C++, and so I would expect the division operation to be integer division and truncate (not round) any decimal, since YPos is an integer, and therefore the result will be the top of the next label (there is other code to make sure we are still on the same page or column).  Unfortunatly VB doesn't do that, and it rounds the results.  This means that if I used more than half the label it will round up, rather than down, and I will skip a label.  I can already think of a couple work-arounds, and I'm currently using one, but there are a LOT of addresses, and this has to run many times before the report can be displayed or printed.  I'd also just like to know how to force integer division in VB.Net.
Thanks.
Joel Coehoorn
Tuesday, July 05, 2005

==================================================================
Assuming it's the same of previous versions of VB, the integer division operator is a backslash:

5 \ 2 = 2

Tuesday, July 05, 2005

================================================================
Thanks.  I knew it had to be something simple like that.
Joel Coehoorn
Tuesday, July 05, 2005

Cmf.Net (C) 2009
• 已標示為解答 2009年4月7日 下午 01:49
2009年4月7日 上午 03:18
• 你要查線上手冊 隱含 自動 轉型。

C# 應該是沿用 C++ 的溢位刪去來轉型，等於是無條件捨去，也就是 VB 的 Int 函數。
VB 的自動轉型從 QB 起就一直是用 CInt 。

所以這邊兩個不同在這裡。

至於求商，你可以用整數除法或是配合 Int 函數。

論壇是網友平等互助 保證解答請至 微軟技術支援服務
• 已標示為解答 2009年4月7日 下午 01:49
2009年4月7日 上午 03:22

### 所有回覆

• 求商可以用 \
743 \ 15 = 49
• 已標示為解答 2009年4月7日 下午 01:50
2009年4月7日 上午 01:32
• ### Integer Division

I'm working on modifying some old code for a report that prints out some address labels.  Language is VB.Net.  The labels are each 1" (or 100 pixels in my case) high and there is a 1/2" (50px) margin at the top.  There is already a variable available that stores the current vertical position of the 'cursor' as I write data to the report.  The piece I'm working on advances the cursor to the top of the next label.  What I want to do is this:
YPos = ((((YPos-50) / 100) + 1) * 100) + 50
I learned to program in C/C++, and so I would expect the division operation to be integer division and truncate (not round) any decimal, since YPos is an integer, and therefore the result will be the top of the next label (there is other code to make sure we are still on the same page or column).  Unfortunatly VB doesn't do that, and it rounds the results.  This means that if I used more than half the label it will round up, rather than down, and I will skip a label.  I can already think of a couple work-arounds, and I'm currently using one, but there are a LOT of addresses, and this has to run many times before the report can be displayed or printed.  I'd also just like to know how to force integer division in VB.Net.
Thanks.
Joel Coehoorn
Tuesday, July 05, 2005

==================================================================
Assuming it's the same of previous versions of VB, the integer division operator is a backslash:

5 \ 2 = 2

Tuesday, July 05, 2005

================================================================
Thanks.  I knew it had to be something simple like that.
Joel Coehoorn
Tuesday, July 05, 2005

Cmf.Net (C) 2009
• 已標示為解答 2009年4月7日 下午 01:49
2009年4月7日 上午 03:18
• 你要查線上手冊 隱含 自動 轉型。

C# 應該是沿用 C++ 的溢位刪去來轉型，等於是無條件捨去，也就是 VB 的 Int 函數。
VB 的自動轉型從 QB 起就一直是用 CInt 。

所以這邊兩個不同在這裡。

至於求商，你可以用整數除法或是配合 Int 函數。

論壇是網友平等互助 保證解答請至 微軟技術支援服務
• 已標示為解答 2009年4月7日 下午 01:49
2009年4月7日 上午 03:22
• 感謝大家的回覆！
尤其是 心冷熱情熄 前輩的原因解說！
2009年4月7日 下午 01:49