How can I convert a string such as 5.7303333333e+02 to decimal in PowerShell? How can I convert a string such as 5.7303333333e+02 to decimal in PowerShell? powershell powershell

How can I convert a string such as 5.7303333333e+02 to decimal in PowerShell?


What about :

[int]"5.7303333333e+02"[decimal]"5.7303333333e+02"


TL;DR:

You need to specify NumberStyles.Float

PS C:\> $s = '5.7303333333333333333333333e+02'PS C:\> $style = [Globalization.NumberStyles]::FloatPS C:\> $culture = [cultureinfo]::GetCultureInfo('en-US')PS C:\> [decimal]::Parse($s, $style)573.03333333333333333333333PS C:\> [decimal]$dec = 0PS C:\> [decimal]::TryParse($s, $style, $culture, [ref] $dec)TruePS C:\> $dec573.03333333333333333333333

or append the d suffix

PS C:\> Invoke-Expression ($s + 'd')573.0333333333333333333333333

As Emperor XLII commented above, [decimal]"5.7303333333e+02" does not actually work because it'll convert to decimal through double. Try adding more digits and you'll immediately realized that the output isn't correct anymore:

PS C:\> Trace-Command TypeConversion { [decimal]'5.7303333333e+02' } -PSHostDEBUG: TypeConversion Information: 0 : Converting to decimal.DEBUG: TypeConversion Information: 0 : Exception converting to decimal: "Input string was not in a correct format.". Converting to decimal passing through double.DEBUG: TypeConversion Information: 0 : Numeric Conversion through System.Double.573.03333333PS C:\> [decimal]'5.7303333333e+02'573.03333333PS C:\> [decimal]'5.730333333333333333333333333e+02' # doesn't work573.033333333333PS C:\> 5.730333333333333333333333333e+02d           # correct value573.0333333333333333333333333

As you can see if we use the d suffix for decimal we'll get the correct high-precision value, so one simple trick is just spawning a new shell to normalize the value although that'll be obviously slow, or use Invoke-Expression

PS C:\> $d = '5.730333333333333333333333333e+02'PS C:\> [decimal]$(powershell -com ($d + 'd'))573.0333333333333333333333333PS C:\> Invoke-Expression ($d + 'd'))573.0333333333333333333333333

But why doesn't TryParse work? The reason is because the default style used by Decimal.Parse and Decimal.TryParse is like this

[ws][sign][digits,]digits[.fractional-digits][ws]

which doesn't allow the exponent. If you read Parse's documentation you'll see that the full number format is

[ws][$][sign][digits,]digits[.fractional-digits][e[sign]digits][ws]

Where

e:

  • The 'e' or 'E' character, which indicates that the value is represented in exponential notation. The s parameter can represent a number in exponential notation if style includes the NumberStyles.AllowExponent flag.

That means you need to use NumberStyles.Float (which includes NumberStyles.AllowExponent) as above to make it work