开发者

How to use locale independent decimal and numeric separator in format string?

开发者 https://www.devze.com 2023-02-17 04:48 出处:网络
I am using the following to format a开发者_StackOverflow中文版 column value (bound to value of type decimal?):

I am using the following to format a开发者_StackOverflow中文版 column value (bound to value of type decimal?):

[DisplayFormat(DataFormatString = "{0:N2}")] 

I want to now change it to the following format :

[DisplayFormat(DataFormatString = "{0:#,###.00##}")]

But, this isn't locale independent as I am hard coding decimal and numeric separator. Basically, I want to display a string with appropriate numeric and decimal separators. Also, I want minimum two zeros after decimal separator and maximum of 4 zeros. Is it possible to specify such a string at compile-time?


But, this isn't locale independent as I am hard coding decimal and numeric separator

No you aren't; in format specifiers the . and , mean "decimal separator" and "thousands separator" respectively, not literal commas and periods. It should work correctly. For example, '#,###.00##' in a fr-FR culture should come out as "1 234,56":

var culture = CultureInfo.GetCultureInfo("fr-fr");
decimal d = 1234.56M;
string s = d.ToString("#,###.00##'", culture); // "1 234,56"
0

精彩评论

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