Home » How to Convert String to Double in PowerShell

How to Convert String to Double in PowerShell

In PowerShell, there are several ways to convert string to double in PowerShell.

The following methods show how you can do it with syntax.

Method 1: Using Convert.ToDouble() method

$doubleNumber = [System.Convert]::ToDouble($stringNumber)

This example will parse the string and convert it to a double.

Method 2: Using Double.TryParse() method

[double]::TryParse($stringNumber,[ref]$doubleValue)

This example will convert the string to a double.

Method 3: Using Casting

$doubleNumber = [double]$stringNumber

This example will convert the string to a double using casting.

Method 4: Using Parse() method

$doubleNumber = [double]::Parse($stringNumber)

The following examples show how you can use each of the methods.

Convert String to Double Using Convert.ToDouble() method

You can use the ToDouble() method to convert the string to a double.

# define a string 
$stringNumber = "293.15"

# convert string to double using ToDouble()
$doubleNumber = [System.Convert]::ToDouble($stringNumber)
Write-Output $doubleNumber

Output:

293.15

In this script, we defined a string variable $stringNumber that contains a number in string type.

To convert the string to a double, we used the ToDouble() method and passed the $stringNumber as an input parameter. It returns the double representation of string variable $stringNumber.

Finally, we use the Write-Output cmdlet to print the number to the console.

Convert String to Double Using TryParse() Method

A safe way to convert a string to a double is to use the [double]::TryParse() method in PowerShell. It attempts to convert the string and returns the true if the conversion succeeds or false if it fails.

# define a string representing double
$stringNumber = "20.02"
$doubleValue = 0
if([double]::TryParse($stringNumber,[ref]$doubleValue)){
    Write-Output "Double value is: $doubleValue"
} else {
   Write-Output "Conversion failed"
} 

Output:

20.02

In this script, the $stringNumber variable contains the string representing double. We used the TryParse() method to convert the string to a double.

Finally, if the conversion succeeds, it prints the output using the Write-Output cmdlet.

Convert String to Double Using Casting

The easy way to convert string to a double is to use casting.

# define a string representing double
$stringNumber = "15.556"
$doubleNumber = [double]$stringNumber
Write-Output $doubleNumber

Output:

15.556

In this script, we used the [double] casting to convert a string to a double value.

Finally, it prints the double value using the Write-Output cmdlet in PowerShell.

Convert String to a Double Using Parse() Method

Another way to convert a string to a double value is to use the Parse() method in PowerShell.

# define a string representing double
$stringNumber = "20.02"
$doubleNumber = [double]::Parse($stringNumber)

Write-Output $doubleNumber

Output:

20.02

In this script, the $stringNumber variable contains the string representing double. We then use [double]::Parse() method which takes $stringNumber as an argument to convert a string to a double value.

Finally, we use the Write-Output cmdlet to print the double value.

Conclusion

I hope the above article on converting a string to a double in PowerShell is helpful to you.

You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.