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

本文关键字:设置 格式 数字 但将 显示 可以使 当前区域性 区域性 是否 | 更新日期: 2023-09-27 17:59:11

我想使用系统定义的当前区域性设置显示货币金额(在DataGridView的单元格中)。全球化CultureInfo。CurrentCulture。NumberFormat除外,我不希望出现零值,即我希望它们保留为空

因此,我想知道是否可以将以下两种显示值的方法结合起来:

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为我处理所有格式。也许我需要将该单元格类型子类化并覆盖一些东西。。。?

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

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

string s = "";
if(amount != 0){
    s = string.Format("{0:c}", amount);
}

我希望这就是您想要的

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

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

编辑这将有助于抑制零

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