3

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)

1 Answer 1

8

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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?
Yes it is the [Math]::Round($val) I mentioned in the answer
YOU ARE THE BEST! yeah [Math]::Round($val) is my solution. (I just saw the edit right now)
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.