I'm developing an MVC3 application and I have the problem that many users according with their preferences prefer dot as decimal separator or comma (for example the download of Excel files using a csv file with changed extension required a specific format for avoiding user have to replace dot for comma and viceversa).
My solution for now is a radiobutton and store everything as string and replace the default by the selected value (comma or dot)
There is another solution which avoid the transformation of everything to string?
Yeah, CultureInfo. Store things internaly using a predefined CultureInfo (whatever is local to your region). Associate the radion button values to cultures that are using dot (english) or comma(french) and then serve the values using the chosen culture info.
The point is you keep the values internally using ONE format then use the desired culture info to display them.
Related
I'm trying to send a decimal number from html to c# via a form, but it gets caught as an integer.does anyone know why that is?
Without all the details, I would check two things:
The model so that it matches
Check to see if I'm taking into account Culture relevant details. In some countries the dot (.) that separates the decimals is not the same. Some cultures use comma (,) to separate decimals and thus, you'll get the integer value, as the default culture fallback.
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.
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)?
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)
I'm attempting to search my MongoDB for string phone numbers that are all in different formats.
E.g. (323)704-3234, 3237043234, 323-704-3234,+1-323-704-3234,323.704.3234, etc.
Is there an operator or regex that I can use that MongoDB provides that allows you to find strings minus special characters?
For example in c#,
collection.Find(Query.Matches("PhoneNumber",(some regex, replace, or where)3237043234))
Sorry, but you are doing it wrong.
You can accept phone strings in any format (if you want so), but before storing them you need to convert them to some specific format. In your case the best will be just to convert it to number (smaller size to store, to create index).
Then one more time, when the user asks for a number - he can also provide it in any format, and before you search for it you will convert it to the number. (Do not forget to put index on phone number field) Being more user friendly, you can show the ouput in the same format the user asked it.
To change all your current phones, just iterate through the database and update each of the numbers.
The requirement goes this way. I have localized strings in my asp.net web application. I have a aspx page. Consider there is a string key-value pair in the resource file. The value is translated in different languages and available in different resource files. Ex: Strings.fr-FR.resx. Consider the value in resource file is "Hello World", that is translated to different languages.
In my aspx page, I want to retrieve the string from the resource file and display it in the page. But, I want "World" only to be in link format. How can i do it? If I display entire string in an anchor tag , then entire word "Hello World" would be in link format.
Again, my question is how to display only "World" in link format after retrieving from the resource file.
Thanks in advance.
Normally you can't do that, if you need a specific string to be localized and you create a resource element for that purpose, than you should consider it as an atomic element. Therefore: create a hello resource item.
If you really need to split that string you can call .ToString(), then .Split() on whitespace and finally take the second element with [1].
But I don't advise you to do this because some languages can have the word corrisponding to world as first word or even using a different number of words to say hello world. Even if you have only N languages, all translating 'hello world' with 2 words and having world as second word, I'd not do that because you are creating a strong relation between string form and its semantic, which is a source of bugs in case of new supported languages or string change.
I have to build a C# program that makes CSV files and puts long numbers (as string in my program). The problem is, when I open this CSV file in Excel the numbers appear like this:
1234E+ or 1234560000000 (the end of the number is 0)
How I retain the formatting of the numbers? If I open the file as a text file, the numbers are formatted correctly.
Thanks in advance.
As others have mentioned, you can force the data to be a string. The best way for that was ="1234567890123". The = makes the cell a formula, and the quotation marks make the enclosed value an Excel string literal. This will display all the digits, even beyond Excel's numeric precision limit, but the cell (generally) won't be able to be used directly in numeric calculations.
If you need the data to remain numeric, the best way is probably to create a native Excel file (.xls or .xlsx). Various approaches for that can be found in the solutions to this related Stack Overflow question.
If you don't mind having thousands separators, there is one other trick you can use, which is to make your C# program insert the thousands separators and surround the value in quotes: "1,234,567,890,123". Do not include a leading = (as that will force it to be a string). Note that in this case, the quotation marks are for protecting the commas in the CSV, not for specifying an Excel string literal.
Format those long numbers as strings by putting a ' (apostrophe) in front or making a formula out of it: ="1234567890123"
You can't. Excel stores numbers with fifteen digits of precision. If you don't mind not having the ability to perform calculations on the numbers from within Excel, you can store them as Text, and all of the digits will display.
When I generate data to imported into Excel, I do not generate a CSV file if I want control over how the data are displayed. Instead, I write out an Excel file where the properties of the cells are set appropriately. I do not know if there is a library out there that would do that for you in C# without requiring Excel to be installed on the machine generating the files, but it is something to look into.
My two cents:
I think it's important to realize there is a difference between "Data" and "Formatting". In this example you are kind of trying to store both in a data-only file. This will, as you can tell from other answers, change the nature of the data. (In other words cause it to be converted to a string. A CSV file is a data only file. You can do some tricks here and there to merge formatting in with data, but to my way of thinking this essentially corrupts the data by merging it with non-data values: ie: "Formatting".
If you really need to be able to store formatting information I suggest that, if you have time to develop it out, you switch to a file type capable of storing formatting info separately from the data. It sounds like this problem would be a good candidate for a XML Spreadsheet solution. In this way you can not only specify your data, but also it's type and any formatting you choose to use.