What is the easiest way to do integer division in PowerShell? So that the arguments are integers and the result is also an integer.
In C++ it would be e.g. 115/10.
What is the easiest way to do integer division in PowerShell? So that the arguments are integers and the result is also an integer.
In C++ it would be e.g. 115/10.
For true integer division, use
System.Math.Truncateand cast the result to[int]:Alternatively, use
System.Math.DivRem, which directly returns an[int](or[long]):In PowerShell (Core) 7+, additional, integer-type specific overloads are available that return tuples, so you may alternatively use:
PowerShell widens numerical types on demand, and switches to
[double]for division with integer operands (of any integer type, such as[int]) that would yield a remainder.Casting to the result of a division to
[int](e.g.[int] (115 / 10)) is not the same as using[Math]::Truncate(), as that performs half-to-even rounding - see this answer for details.