How to Convert String to Double in PowerShell?

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 $double

In 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:

Convert String to Double in PowerShell

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 $double

This method is particularly useful if you need to handle specific culture settings or formats.

You can see the output in the screenshot below:

PowerShell Convert String to Double

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:

How to Convert String to Double in PowerShell

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:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.