Converting VBScript's Integer Division Operator

Windows PowerShell

Definition: Divides two numbers and returns an integer result.

Integer Division Operator

In VBScript the integer division operator returns only the integer portion of an answer, stripping off decimal points without rounding the number either up or down. For example, 100 / 26 is equal to 3.84615384615385; however 100 \ 26 is equal to 3 (just the integer portion of the answer).

Note. OK, so that’s not 100% true; that’s only true for whole numbers. Given a fractional value, that value will be rounded up or down before division takes place. But that’s all right; we’ve accounted for that by converting the values in the equation to integers before we divide them.

You can perform integer division in Windows PowerShell by using the .NET Framework class System.Math and the Floor method. The following command divides 100 by 26 and then stores the integer portion of the answer in the variable $a:

$a = [math]::floor([int] 100 / [int] 26)

When you run this command and then echo back the value of $a you should get the following:


Return to the VBScript to Windows PowerShell home page