CultureInfo 的 NumberFormat.PercentPositivePattern 已在我的 Windows 10 机器上更改

CultureInfo's NumberFormat.PercentPositivePattern has been changed on my Windows 10 Machine

当 selenium 在我的本地机器上测试 .net 核心应用程序时,我注意到我的百分比字符串 (.ToString("p2")) 在数字和 % 之间没有显示 space,不同于我们测试服务器的页面。经过一些研究,我的 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。

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

确实,en-US 文化中的百分比格式在 Windows10 的最新版本中发生了变化[1 ]

Windows 7:

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

Windows10,版本 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 %

注意:在WindowsPowerShell/.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所述:

Culture-specific formatting can and will change over time.

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

Unlike culture-sensitive data, which is subject to change by user customization or by updates to the .NET Framework or the operating system, invariant culture data is stable over time and across installed cultures and cannot be customized by users. This makes the invariant culture particularly useful for operations that require culture-independent results, such as formatting and parsing operations that persist formatted data, or sorting and ordering operations that require that data be displayed in a fixed order regardless of culture.


[1] Sean1215(OP)报告说 变化一定发生在 OS build 14393 之后和 16299 之前的某个时间。由于其组织中不同团队基于组策略的 Windows 更新计划不同,他的计算机碰巧使用了比同事更新的版本。