Reputation: 609
I have an Array where some drive data from WMI are captured:
$drivedata = $Drives | select @{Name="Kapazität(GB)";Expression={$_.Kapazität}}
The Array has these values (2 drives):
@{Kapazität(GB)=1.500} @{Kapazität(GB)=1.500}
and just want to convert the 1.500 into a number 1500
I tried different suggestions I found here, but couldn't get it working:
-Replace ".","" and [int]
doesn't work.
I am not sure if regex would be correct and how to do this.
Upvotes: 43
Views: 264830
Reputation: 360
Combining some of the answers already provided here, I ended up using the following code which works with both string and numeric values:
$Converted = '5'
If ([Int]::TryParse($Converted , [Ref] $Null))
{
$Converted = [Convert]::ToInt32($Converted)
Write-Host "Pass '$Converted' $($Converted.GetType())" -ForeGroundColor Green
} Else {
Write-Host "Fail '$Converted' $($Converted.GetType())" -ForeGroundColor Red
}
Upvotes: 0
Reputation: 2293
Replace all but the digits in the string like so:
$messyString = "Get the integer from this string: -1.500 !!"
[int]$myInt = $messyString -replace '\D', ''
$myInt
# PS > 1500
The regex \D
will match everything except digits and remove them from your string.
This will work fine for your example.
Upvotes: 3
Reputation: 4233
I demonstrate how to receive a string, for example "-484876800000" and tryparse the string to make sure it can be assigned to a long. I calculate the Date from universaltime and return a string. When you convert a string to a number, you must decide the numeric type and precision and test if the string data can be parse, otherwise, it will throw and error.
function universalToDate
{
param (
$paramValue
)
$retVal=""
if ($paramValue)
{
$epoch=[datetime]'1/1/1970'
[long]$returnedLong = 0
[bool]$result = [long]::TryParse($paramValue,[ref]$returnedLong)
if ($result -eq 1)
{
$val=$returnedLong/1000.0
$retVal=$epoch.AddSeconds($val).ToString("yyyy-MM-dd")
}
}
else
{
$retVal=$null
}
return($retVal)
}
Upvotes: 1
Reputation:
Simply divide the Variable containing Numbers as a string by 1. PowerShell automatically convert the result to an integer.
$a = 15; $b = 2; $a + $b --> 152
But if you divide it before:
$a/1 + $b/1 --> 17
Upvotes: 32
Reputation: 173
Since this topic never received a verified solution, I can offer a simple solution to the two issues I see you asked solutions for.
The string class offers a replace method for the string object you want to update:
Example:
$myString = $myString.replace(".","")
The system.int32 class (or simply [int] in powershell) has a method available called "TryParse" which will not only pass back a boolean indicating whether the string is an integer, but will also return the value of the integer into an existing variable by reference if it returns true.
Example:
[string]$convertedInt = "1500"
[int]$returnedInt = 0
[bool]$result = [int]::TryParse($convertedInt, [ref]$returnedInt)
I hope this addresses the issue you initially brought up in your question.
Upvotes: 12
Reputation: 207
It seems the issue is in "-f ($_.Partition.Size/1GB)}}" If you want the value in MB then change the 1GB to 1MB.
Upvotes: -2
Reputation: 1759
Simply casting the string as an int
won't work reliably. You need to convert it to an int32
. For this you can use the .NET convert
class and its ToInt32
method. The method requires a string
($strNum
) as the main input, and the base number
(10
) for the number system to convert to. This is because you can not only convert to the decimal system (the 10
base number), but also to, for example, the binary system (base 2).
Give this method a try:
[string]$strNum = "1.500"
[int]$intNum = [convert]::ToInt32($strNum, 10)
$intNum
Upvotes: 67