1

I am writing a PowerShell script that converts a number with a decimal point into an integer.

$val = 1024.24

How to convert this value to integer? (I want it to be 1024)

Se7en
  • 363
  • 4
  • 13

1 Answers1

4

Use floor rounding, which rounds to the lower whole number

[Math]::Floor($val)

Edit: if just discarding decimal part is not what you are looking for you can use [Math]::Round($val) which will round the number like normal math rounding or you can use [Math]::Ceiling($val) which will round up (in your case it will round to 1025) and it is probably not what you need but it is good to know that you have these options as well.

Vojin Purić
  • 2,140
  • 7
  • 9
  • 22
  • Thank you! This actually worked. Any ideas for converting a decimal value to the nearest integer? I mean if the value is ```$val = 1.9``` for example, then it is rounded up to ```$val = 2.0```. Does PowerShell have this capability? – Se7en Nov 24 '21 at 18:12
  • 1
    Yes it is the `[Math]::Round($val)` I mentioned in the answer – Vojin Purić Nov 24 '21 at 18:12
  • 1
    YOU ARE THE BEST! yeah ```[Math]::Round($val)``` is my solution. (I just saw the edit right now) – Se7en Nov 24 '21 at 18:17
  • 1
    @Se7en, note that casting to `[int]` (or `[long]`, or `[decimal]`, or `[bigint]`, ...) is also an option, but that notably performs _half-to-even_ rounding with `.5` values; e.g. `[int] 1.5` is `2`, but so is `[int] 2.5`, because the nearest _even_ integer is `2`. – mklement0 Nov 24 '21 at 23:05