在 Selenium 测试本地计算机上的.net核心应用程序时,我注意到我的百分比字符串(.ToString("p2"))显示数字和%之间没有空格,这与测试服务器的页面不同。经过研究后,似乎Windows 10机器上的区域性信息已有所更改。有谁知道如何将其重置为默认值?或更改设置?

get-culture

LCID             Name             DisplayName
----             ----             -----------
1033             en-US            English (United States)


(get-culture).NumberFormat

CurrencyDecimalDigits    : 2
CurrencyDecimalSeparator : .
IsReadOnly               : True
CurrencyGroupSizes       : {3}
NumberGroupSizes         : {3}
PercentGroupSizes        : {3}
CurrencyGroupSeparator   : ,
CurrencySymbol           : $
NaNSymbol                : NaN
CurrencyNegativePattern  : 0
NumberNegativePattern    : 1
PercentPositivePattern   : 1
PercentNegativePattern   : 1
NegativeInfinitySymbol   : -∞
NegativeSign             : -
NumberDecimalDigits      : 2
NumberDecimalSeparator   : .
NumberGroupSeparator     : ,
CurrencyPositivePattern  : 0
PositiveInfinitySymbol   : ∞
PositiveSign             : +
PercentDecimalDigits     : 2
PercentDecimalSeparator  : .
PercentGroupSeparator    : ,
PercentSymbol            : %
PerMilleSymbol           : ‰
NativeDigits             : {0, 1, 2, 3…}
DigitSubstitution        : None

PercentPositivePattern和PercentNegativePattern设置为1而不是0。此外,当其他框显示为false时,IsReadOnly似乎为true。

检查了我的地区信息。一切看起来都正确。

最佳答案

实际上,在最新版本的Windows 10中,en-US文化中百分比的格式已更改:[1]

Windows 7的:

PS> (1).ToString("p2")
100.00 %  # Space between number and "%"

Windows 10版本1903:
PS> (1).ToString("p2")
100.00%   # NO space between number and "%"

要将旧线程的行为恢复为仅(不是全局的,不是持久的),可以执行以下操作:
$c = [cultureinfo]::CurrentCulture.Clone()  # Clone the current culture.
$c.NumberFormat.PercentPositivePattern = 0  # Select the old percentage format.
$c.NumberFormat.PercentNegativePattern = 0  # For negative percentages too.
[cultureinfo]::CurrentCulture = $c  # Make the cloned culture the current one.

此后,(1).Tostring('p2')再次产生100 %

注意:在Windows PowerShell/.NET Framework中,您也可以直接修改[cultureinfo]::CurrentCulture的属性(无需克隆)。尽管这简化了解决方案,但是请注意,PowerShell Core/.NET Core不再支持该解决方案,因为预定义区域性是只读的。
# Windows PowerShell / .NET Framework (as opposed to  .NET Core) ONLY
PS> [CultureInfo]::CurrentCulture.NumberFormat.PercentPositivePattern = 0; (1).ToString("p2")
100.00 %

退后一步:

Eric MSFT在评论中所述:



为了确保跨时间和跨文化格式的稳定性,您应该使用不变文化 InvariantCulture (添加了强调):



[1] Sean1215(OP)报告更改一定发生在OS build 14393之后和16299 之前。由于组织中各个团队之间基于组策略的Windows更新时间表不同,因此他的计算机使用的版本比同事使用的版本更新。

关于windows - 在Windows 10计算机上更改了CultureInfo的NumberFormat.PercentPositivePattern,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58827767/

10-13 07:46
查看更多