是否可以使用当前区域性设置格式化数字,但将 0 显示为空白?

发布于 2024-11-15 16:28:28 字数 708 浏览 2 评论 0原文

我想使用 System.Globalization.CultureInfo.CurrentCulture.NumberFormat 定义的当前区域性设置来显示货币金额(在 DataGridView 的单元格中),但我不希望出现零值 - 即我希望它们留空

所以,我想知道是否可以结合以下两种方法来显示值:

string.format("{0:c}", 1)  // will show $1.00 on a machine with typical English (US) settings

但是;

string.format("{0:c}", 0)  // will show $0.00 on a machine with typical English (US) settings

我知道我可以使用它来实现我想要的目标;

string.format("{0:"$##,##0.00";($##,##0.00);''}", 0)

然而,据我了解,这对文化环境并不敏感。

两者都有可能实现,如果可以的话,如何实现?

我正在寻找一个可以实现的解决方案,在最好的情况下通过设置 DataGridViewCell 的 format 属性,从而允许 DataGridView 为我处理所有格式。也许我需要对该单元格类型进行子类化并覆盖某些内容......?

I would like to display currency amounts (in cells of a DataGridView) using the current culture settings as defined by System.Globalisation.CultureInfo.CurrentCulture.NumberFormat except, I do not want zero values to appear - i.e. I want them to be left blank

So, I want to know if it possible to combine the following two approaches to displaying values:

string.format("{0:c}", 1)  // will show $1.00 on a machine with typical English (US) settings

BUT;

string.format("{0:c}", 0)  // will show $0.00 on a machine with typical English (US) settings

I understand I can achieve what I want using;

string.format("{0:"$##,##0.00";($##,##0.00);''}", 0)

however as I understand it this will not be sensitive to cultural settings.

It is possible to achieve both and if so, how?

I am searching for a solution that can be implemented, in the best case by setting the format property of a DataGridViewCell and thereby allowing the DataGridView to take care of all the formatting for me. Maybe I need to subclass that cell type and override something...?

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

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

发布评论

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

评论(3

你对谁都笑 2024-11-22 16:28:28

您可以创建自定义格式提供程序,然后将其用于 Format 属性。
这应该有帮助 http://msdn.microsoft.com/en-我们/library/system.iformatprovider.aspx#Y990

You can create a custom format provider and then use it for the Format property.
This should help http://msdn.microsoft.com/en-us/library/system.iformatprovider.aspx#Y990

萌逼全场 2024-11-22 16:28:28
string s = "";
if(amount != 0){
    s = string.Format("{0:c}", amount);
}
string s = "";
if(amount != 0){
    s = string.Format("{0:c}", amount);
}
昨迟人 2024-11-22 16:28:28

我希望这就是您正在寻找的

CultureInfo cinfo = CultureInfo.CreateSpecificCulture(CultureInfo.CurrentCulture.Name);
cinfo.NumberFormat.CurrencyDecimalDigits = 0;

现在您已经在 CultureInfo 类中进行了所需的修改,现在将此 CultureInfo 分配回 CurrentCultureCurrentUICulture code> 你应该可以开始

编辑
这将帮助您抑制零

string.Format("{0}{1:#.#}", Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencySymbol, value);

I hope this is what you are looking for

CultureInfo cinfo = CultureInfo.CreateSpecificCulture(CultureInfo.CurrentCulture.Name);
cinfo.NumberFormat.CurrencyDecimalDigits = 0;

Now you have the required modifications made in the CultureInfo class, Now assign this CultureInfo back to the CurrentCulture and CurrentUICulture and you should be good to go

EDIT
This would help you in suppressing zero

string.Format("{0}{1:#.#}", Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencySymbol, value);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文