.net 6 将负十进制数转换为与 .net48 不同的货币

问题描述 投票:0回答:1

我有一个将十进制转换为字符串货币的方法

public static string ToCurrencyWithParentheses(this decimal initDecimal)
{
    return initDecimal.ToString("C", CultureInfo.GetCultureInfo("en-US"));
}

我们最近在一个项目中从 .net48 切换到 .net6,我注意到该方法开始以不同的方式工作。在 .net48 上,我会将负数

-58.45
传递到方法中并得到
($58.45)
,但现在在 .net6 上我得到
-$58.45
。 谁能告诉我如何修复该方法而不进行错误编码,以便正确处理数字?或者这是一个错误?

我无法通过谷歌搜索现成的解决方案。我尝试使用 string.Format() 而不是 ToString() 之类的东西,但没有任何效果。

c# .net string format .net-6.0
1个回答
0
投票

将这些行添加到您的项目文件中:

<ItemGroup>
  <RuntimeHostConfigurationOption Include="System.Globalization.UseNls" Value="true" />
</ItemGroup>

参见 使用 NLS 代替 ICU

© www.soinside.com 2019 - 2024. All rights reserved.