Reputation: 589
I'm working on a project that uses decimals in a textbox. I'm currently developing in a machine that has decimal separators set to "," instead of "." so I had to use this sentence when parsing text string into decimal:
decimal number = Decimal.Parse(number.Text, CultureInfo.InvariantCulture);
Now... Is CultureInfo.InvariantCulture the right thing to do or should I use CurrentCulture instead?
Thank you,
Matias.
Upvotes: 4
Views: 2442
Reputation: 111940
If you do a correctly internationalized program...
A) If you are using Winforms or WPF or in general the user will "work" on the machine of the program, then input parsing should be done with the CurrentCulture
B) If you are web-programming, then the user should be able to select its culture, and CurrentCulture
(the culture of the web-server) should be only used as a default
And then,
A) data you save "internally" (so to be written and then read by your program) should use InvariantCulture
,
B) data you interchange with other apps would be better to be written with InvariantCulture
(unless the other app is badly written and requires a specific format),
C) files you export to the user should follow the previous rules (the ones about user interface), unless you are using a "standard-defined format" like XML (then use Xml formatters)
Upvotes: 0
Reputation: 63772
For user input, you usually want CultureInfo.CurrentCulture
. The fact that you're using a locale that's not natural to you is not the usual user case - if the user has ,
as the decimal point in their whole system, they're probably used to using that, instead of your preferred .
. In other words, while you're testing on a system with locale like that, learn to use ,
instead of .
- it's part of what makes locale testing useful :)
On the other hand, if you're e.g. storing the values in a configuration file or something like that, you really want to use CultureInfo.InvariantCulture
.
The thinking is rather simple - is it the user's data (=> CurrentCulture
), or is it supposed to be global (=> InvariantCulture
)?
Upvotes: 4