Recently, one of my team members was struggling to convert a string to double in PowerShell. I suggested a few methods and showed some examples. In this tutorial, I will explain how to convert string to double in PowerShell using various methods.
To convert a string to a double in PowerShell, you can use type casting by prefixing the string with [double], e.g., $double = [double]$string. Alternatively, use [double]::Parse($string) for more control, or the -as operator for safe conversion that returns $null if it fails.
Convert String to Double in PowerShell
PowerShell provides different methods to convert a string to double. Let us check each method with examples.
Method 1: Using Type Casting
The simplest way to convert a string to a double in PowerShell is by using type casting. PowerShell allows you to cast a string variable to a double by prefixing it with [double].
Here is a simple example to understand it better.
$string = "123.45"
$double = [double]$string
Write-Output $doubleIn this example, the string "123.45" is cast to a double, and the output will be 123.45.
I executed the above PowerShell script, and you can see the output in the screenshot below:

Read Convert String to Hashtable in PowerShell
Method 2: Using [double]::Parse()
Another method to convert a string to a double is by using the [double]::Parse() method in PowerShell. This method is part of the .NET framework and provides more control over the conversion process.
Here is an example.
$string = "123.45"
$double = [double]::Parse($string)
Write-Output $doubleThis method is particularly useful if you need to handle specific culture settings or formats.
You can see the output in the screenshot below:

Method 3: Using Try/Catch for Error Handling
It’s essential to include error handling when dealing with user input or data that might not always be in the correct format. You can use a try/catch block to handle any exceptions that may occur during the conversion.
Here is an example.
$string = "123.45"
try {
$double = [double]$string
Write-Output $double
} catch {
Write-Output "Conversion failed. Please ensure the input is a valid number."
}If the conversion fails, the catch block will handle the error gracefully, providing a meaningful message to the user.
Check out Convert String to JSON in PowerShell
Method 4: Using -as Operator
The -as operator is another way to attempt the conversion. It returns $null if the conversion is not possible, making it a useful tool for conditional logic.
Here is an example.
$string = "123.45"
$double = $string -as [double]
if ($double -ne $null) {
Write-Output $double
} else {
Write-Output "Conversion failed."
}This method is particularly useful when you need to check if the conversion was successful without throwing an exception.
I also executed the above code, and you can see the output in the screenshot below:

Method 5: Convert String Fractions to Doubles
Sometimes, you might encounter strings that represent fractions, such as "1/2". Converting these to doubles requires a bit more work.
Here is an example.
$string = "1/2"
if ($string -match "(\d+)/(\d+)") {
$numerator = [double]$matches[1]
$denominator = [double]$matches[2]
$double = $numerator / $denominator
Write-Output $double
} else {
Write-Output "Invalid fraction format."
}In this example, we use a regular expression to extract the numerator and denominator, then perform the division to get the double value.
Conclusion
In this tutorial, I have explained different methods to convert string to double in PowerShell. Here are the methods:
- Type Casting is straightforward and easy to use.
- [double]::Parse() offers more control and flexibility.
- Try/Catch provides robust error handling.
- -as Operator is useful for conditional checks.
- String Fractions can be handled with regular expressions.
I hope this helps. If you face any issues, feel free to leave a comment below with the error screenshot.
You may also like the following tutorials:
- How to Convert String to Int in PowerShell?
- Convert String to Boolean in PowerShell
- Convert Strings to Lowercase or Uppercase in PowerShell
- How to Convert Object to String in PowerShell?
- Convert String to Base64 in PowerShell
- How to Convert String to Object in PowerShell?
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.