CultureInfo.NumberFormat 隐藏逗号

发布于 2024-09-26 08:21:21 字数 848 浏览 3 评论 0原文

我正在尝试创建一个根本不使用组的 NumberFormat。

我希望显示的所有数字都没有逗号。

例如:

  • 1999 而不是 1,999
  • 2000000 而不是 2,000,000
  • 等等...

不幸的是,我使用的是第 3 方控件,它是一个 NumericEditor,它应用了 CultureInfo 设置来显示逗号。因此,我需要创建一个根本不使用分组的 CultureInfo 实例。

我已经尝试过这个:

int[] groupSize = {0};
CultureInfo culture = new CultureInfo("en-US");
culture.NumberFormat.NumberGroupSizes = groupSize;

另外...

 CultureInfo culture = new CultureInfo("en-US");
 culture.NumberFormat.NumberGroupSeparator = String.Empty; // Throws and exception with the 3rd party control

我得到的最接近的是...

 CultureInfo culture = new CultureInfo("en-US");
 culture.NumberFormat.NumberGroupSeparator = " ";

我根本不喜欢这个解决方案,因为它不是逗号而是空格,而且看起来确实很奇怪。

有什么想法吗?

I am trying to create a NumberFormat that will not use Groups at all.

I would like all numbers to be displayed with NO commas.

Example:

  • 1999 instead of 1,999
  • 2000000 instead of 2,000,000
  • etc...

Unfortunately, I am using a 3rd Party control that is a NumericEditor and it applies a CultureInfo setting on it to show commas. So I need to create a CultureInfo instance that doesn't use Grouping at all.

I have tried this:

int[] groupSize = {0};
CultureInfo culture = new CultureInfo("en-US");
culture.NumberFormat.NumberGroupSizes = groupSize;

Also...

 CultureInfo culture = new CultureInfo("en-US");
 culture.NumberFormat.NumberGroupSeparator = String.Empty; // Throws and exception with the 3rd party control

The closest I have gotten is...

 CultureInfo culture = new CultureInfo("en-US");
 culture.NumberFormat.NumberGroupSeparator = " ";

I don't like this solution at all because instead of a comma its white space and it definitely looks odd.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幽梦紫曦~ 2024-10-03 08:21:21

您正在使用什么第 3 方控件,您是否尝试过反射(使用 .NET Reflector 之类的东西)来查看它们如何使用 CultureInfo 类,因为在常规的旧 C# 代码中,设置 NumberFormat.NumberGroupSize = new int[]{0 };与 NumberFormat.NumberGroupSeperator = String.Empty; 一样有效。看来第 3 方控件可能以非标准方式使用 CultureInfo 属性。

What 3rd-Party control are you using and have you tried reflecting (using something like .NET Reflector) to see how they are using the CultureInfo class, because in regular old C# code, setting the NumberFormat.NumberGroupSize = new int[]{0}; works as well as NumberFormat.NumberGroupSeperator = String.Empty;. It seems the 3rd Party control might be using the CultureInfo properties in a non-standard way.

王权女流氓 2024-10-03 08:21:21

考虑实施自定义格式化程序

您可以像这样分配自定义格式提供程序:

CultureInfo.CurrentCulture.NumberFormat = new MyCustomFormatter();

如果他们(第 3 方控件开发人员)没有实现一些“魔法”,这可能会起作用。

Consider implementing custom formatter.

You can assign your custom format provider like this:

CultureInfo.CurrentCulture.NumberFormat = new MyCustomFormatter();

That might work if they (3rd party control developers) didn't implement some "magic".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文