是否可以使用当前区域性设置格式化数字,但将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以创建自定义格式提供程序,然后将其用于 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
我希望这就是您正在寻找的
现在您已经在 CultureInfo 类中进行了所需的修改,现在将此
CultureInfo
分配回CurrentCulture
和CurrentUICulture
code> 你应该可以开始编辑
这将帮助您抑制零
I hope this is what you are looking for
Now you have the required modifications made in the CultureInfo class, Now assign this
CultureInfo
back to theCurrentCulture
andCurrentUICulture
and you should be good to goEDIT
This would help you in suppressing zero