简而言之:
在使用Parse
或TryParse
时,您需要指定NumberStyles.Float
。
<i>PS C:\></i> $s = '5.7303333333333333333333333e+02'
<i>PS C:\></i> <b>$style = [Globalization.NumberStyles]::Float</b>
<i>PS C:\></i> $culture = [cultureinfo]::GetCultureInfo('en-US')
<i>PS C:\></i> <b>[decimal]::Parse($s, $style)</b>
573.03333333333333333333333
<i>PS C:\></i> [decimal]$dec = 0
<i>PS C:\></i> <b>[decimal]::TryParse($s, $style, $culture, [ref] $dec)</b>
True
<i>PS C:\></i> $dec
573.03333333333333333333333
或者直接添加
d
后缀
PS C:\> Invoke-Expression ($s + 'd')
573.0333333333333333333333333
根据
XLII皇帝的评论,
[decimal]"5.7303333333e+02"
实际上是不起作用的,因为它会通过双精度转换为十进制。尝试添加更多位数,你会立即意识到输出不再正确。
PS C:\> Trace-Command TypeConversion { [decimal]'5.7303333333e+02' } -PSHost
DEBUG: 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.03333333
PS C:\> [decimal]'5.7303333333e+02'
573.03333333
PS C:\> [decimal]'5.730333333333333333333333333e+02'
573.033333333333
PS C:\> 5.730333333333333333333333333e+02d
573.0333333333333333333333333
正如您所看到的,如果我们使用 d
后缀 表示十进制数,我们将得到正确的高精度值。因此,一个简单的技巧就是产生一个新的 shell 来规范化该值,尽管显然会很慢,或者使用 Invoke-Expression。
PS C:\> $d = '5.730333333333333333333333333e+02'
PS C:\> [decimal]$(powershell -com ($d + 'd'))
573.0333333333333333333333333
PS C:\> Invoke-Expression ($d + 'd'))
573.0333333333333333333333333
为什么 TryParse
不起作用?原因是因为Decimal.Parse和Decimal.TryParse使用的默认样式是这样的
[ws][sign][digits,]digits[.fractional-digits][ws]
不允许指数。如果你阅读Parse的文档,你会看到完整的数字格式是
[ws][$][sign][digits,]digits[.fractional-digits][e[sign]digits][ws]
其中 [e]
是
这意味着你需要像上面那样使用 NumberStyles.Float
(其中包括 NumberStyles.AllowExponent
)来使其工作
[decimal]::TryParse("5,7303333333e+02", [ref] $dec)
。问题在于不支持科学计数法,因为[decimal]::TryParse("5,7303333333", [ref] $dec)
可以工作... 请参见:http://msdn.microsoft.com/en-us/library/9zbda557.aspx - Davor Josipovic[decimal]::TryParse
却没有。不过,使用它比起使用 TryParse 看起来更简洁。 - rtf[decimal]'5.730333333333333333333333333e+02'
将返回一个不正确的值。 - phuclv