Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of CultureInfo.InvariantCulture

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.

like image 280
Matias Avatar asked Sep 14 '25 03:09

Matias


1 Answers

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)?

like image 59
Luaan Avatar answered Sep 16 '25 19:09

Luaan