0

If I've a variable called $number and I've the following Cmdlet:

Write-host "The final number is $number"

Is there a method to set it to integer within the write-host Cmdlet.

2
  • 4
    It's unclear what you want. Do you want some kind of custom formatting of the value, or its actual .NET type name? You can get the latter with $number.GetType().Name, but there's rarely a need to do this. In particular, checking if $number is an int is better done with $number is [int]. Commented Dec 13, 2018 at 14:11
  • Better define $Number as integer before or when you use it the first time. That way the calculations will work better and you won't end up having an array. Commented Dec 13, 2018 at 14:39

3 Answers 3

3

I'm not sure what you are trying to accomplish.

If we do this:

$numberAsString = '1'
$number.GetType()

then the output of GetType() will be a string type. If instead we do this:

$numberAsInteger = 1
$number.GetType()

then the output of GetType() will be an integer type.

You can also do the following (this example doesn't really make sense when the assignment is explicit, but as was pointed out in the comments this can be useful if you are accepting user provided values):

[int]$number = '1'

This will now be an integer even though you assigned the value as a string. But none of this seems to matter for the output of write-host. For example you can do the following:

$number = '2'
Write-Host ('The final number is ' + ([int]$number))

This will work, but the output will be the same, and $number would still be a string type in this case.

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

4 Comments

[int]$number = '1' is perfectly fine and even useful. It makes $number strongly typed as int. Whatever you assign will be converted to int. You will be able to do $numberAsInt = "text" but NOT $number = "text"
I guess my point was, if you want $number to be an int, then why would you assign a string (a number in quotes) to it? Wouldn't [int]$number = 1 make more sense?
2 Examples: [int]$number = Read-Host, [int]$number = '12e3' But yes, prefer number literals when possible.
@marsze you are correct of course. I was literally only thinking about a case where you are assigning a known value, like in the original post rather than taking unknown user input. Thanks for the helpful comment.
0

Very unclear what you're asking, but taking your question literally then yes, you can:

Write-host "The final number is $([int]$number = 42)"
# OUTPUT:
# The final number is 42

If you mean "convert to int" then yes, this is also possible:

$number = 12.34
Write-host "The final number is $([int]$number)"
# OUTPUT:
# The final number is 12

Comments

0

To insert an integer in a string you can use:

write-host 'The final number is {0}' -f ([int]$Number)

or

write-host "The final number is $([int]$Number)"

In both cases the $Number is converted to [int] before the string is formed. But when the string is being formed the [int]$Number ist cast .toString()anyway.

Comments

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.