Convert to Numeric

by Jan 18, 2012

Whenever PowerShell asks for user input or reads text file content, the results are text strings. If you expect numbers and want to calculate, make sure you cast them to a numeric format. Have a look:

PS> $number = Read-Host 'Enter a number'
Enter a number: 100
PS> $number * 12
100100100100100100100100100100100100

Now try the same with casting:

PS> $number = [Double](Read-Host 'Enter a number')
Enter a number: 100
PS> $number * 12
1200

Twitter This Tip!
ReTweet this Tip!