开发者

Avoiding localiztion on formatting numbers written to file

开发者 https://www.devze.com 2022-12-17 19:44 出处:网络
I 开发者_开发知识库have a program that sometimes is used in locales that use commas for the decimal separator.Its nice how C# handles all that (good for the UI), but when I export to a file, I need to

I 开发者_开发知识库have a program that sometimes is used in locales that use commas for the decimal separator. Its nice how C# handles all that (good for the UI), but when I export to a file, I need to always use a ".", not the locale specific number. Currently, I do:

String.Format("{0:0.####},{1:0.####}", x, y)

Problem is that in some locales, that ends up using commas instead of periods. Question is, is there a format code that says "always use period", or is the only solution to mess with one's locale?


Simply use:

String.Format(CultureInfo.InvariantCulture, "{0:0.####},{1:0.####}", x, y)


I'd recommend trying FxCop (or the Code Analysis feature of the Team variants of VS2005+). It generates a lot of noise (false positives), but does provide a lot of good practice.

Including: always use the overload that takes an IFormatProvider parameter if one is available - typically you will use CultureInfo.CurrentCulture for formatting output for the user, and CultureInfo.InvariantCulture for storing data in files, the registry etc.

0

精彩评论

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