开发者

Is it possible to format a number using the Current Culture settings but show 0 as blank?

开发者 https://www.devze.com 2023-03-13 13:25 出处:网络
I wo开发者_JAVA百科uld like to display currency amounts (in cells of a DataGridView) using the current culture settings as defined by System.Globalisation.CultureInfo.CurrentCulture.NumberFormat excep

I wo开发者_JAVA百科uld 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...?


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


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


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号