Well... i search the web and found many solutions for the other way, but none for these.
I have a application which gets different currencys by the user. I dont know the currencys in before, it could be everything (russian rubels, usd, €, Yen...)
I need to convert the amount into a decimal, for that i need the current culture. My current solution is very bad 8and incomplete, cause i cant cover all cultures that way), it just checks the currency sign.
if (currency.Contains("zł"))
{
cult = CultureInfo.GetCultureInfo("PL-pl")
}
else if (currency.Contains("$"))
{
//blah blah blah
}
Is there a possiblility to get Culture base on the currency sign. Another maybe difficult thing is, that i dont know if the currency symbol is before or beyond the amount (varys by culture i.E: $45.00 <-> 45.00€)
Create a lookup once and use it for fast access. Notice that a particular currency symbol may be used by multiple cultures:
ILookup<string, CultureInfo> cultureByCurrency =
CultureInfo.GetCultures(CultureTypes.AllCultures)
.ToLookup(_ => _.NumberFormat.CurrencySymbol);
Then to lookup $ for example:
IEnumerable<CultureInfo> culturesThatUseDollar = cultureByCurrency["$"];
There is no exact mapping from a currency code or symbol to a culture. Consider basic examples like EUR (€), which is used as the official currency in 18 countries. There are many issues arising from this mere fact, like whether the symbol is placed before or after the value etc. You should ask the user about the specific formatting to use instead of trying to deduce it from the currency symbol.
Also, a single currency symbol is used for many currencies. Consider that $ can denote both USD, CAD, AUD and other currencies that call themselves as 'dollars'. You should use currency codes if you want an exact specification of a currency.
It is not possible.
EUR for example would map to de-DE, fr-FR, nl-NL and other countries.
There is no mapping from Currency to culture, because multiple countries share currencies
In your else if block, which culture would you assign after finding the $? en-US? fr-CA?
I would suggest a different approach that would remove any sort of ambiguity. Have the user specify their nationality before entering this chunk of code. Consider having the culture information given to you instead of attempting to guess it.
return CultureInfo.GetCultures(CultureTypes.AllCultures).Where(c => c.NumberFormat.CurrencySymbol.Equals("$"));
Related
I am new to C # and I am currently having problems with the following. In C #, I have a Pi floating point number and I want to convert it to a string using the ToString() method. But the conversion gives a string result with a comma "3,1415". On another machine, the same gives the string result with the dot "3.1415". What is the reason for this and what should I do to get a dotted string result?
EDIT: The problem is, I can't change the code, but I can install and uninstall .Net frameworks, change my OS settings, etc.
Edit: if you can't change the code. Change the language/localization of the system to one which uses dot as decimal separator. In Control Panel or Settings.
You should look at internationalization and localization in the System.Globalization namespace.
The advice here is to use one CultureInfo specific for parsing numbers or writing numbers to string.
var flt = 232.23f;
var str = flt.ToString(CultureInfo.InvariantCulture); //for example, you can use CultureInfo.CurrentCulture
This allows you to keep the ThreadCulture without change it.
But take a look at this link https://learn.microsoft.com/en-us/dotnet/api/system.globalization.cultureinfo?view=net-5.0 .Take your time, is dense.
I would just set the current culture at the entry point of your program.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");
However I would also check the regional settings and so forth on the machine where the comma separator appears.
It is related with current culture info. You can specify the culture info in ToString method as a parameter like;
var convertedFloat = floatVariable.ToString(new CultureInfo("en-GB"));
Thanks to GSerg, for comment about Windows regional settings. That solves my problem. In the Windows Control panel enter Region and Language. In the Formats tab click Additional Settings and in the Decimal symbol field specify what decimal separator must be used when converting a floating point number to a string.
I have a loop in which I call ToString() on the int variable i.
for (int i = 1; i <= n; i++)
{
#Html.ActionLink(#i.ToString(), "Index")
}
Resharper tells me that I should:
Specify a culture in string conversion explicitly.
Why is this?
How many ways are there to convert an int into a string?
I would have thought only 1.
There are different methods of grouping symbols, like 1000; 1 000 and 1'000.
Besides there are different digits used for numbers in different countries
Chinese numerals
You can see different numbers in Control Panel -> region and language -> Formats -> additional settings -> Standart digits. (Win 7)
Generally, it's a good practice to always specify explicitly whether you want to use the current culture or if the data your processing is culture invariant. The problem is that if you are processing data which is only processed by your software and not presented to the user in any way (for example database identifiers), then you might run into problems if the data is different on different machines.
For example a database identifier may be serialized on a machine with some culture and deserialized on a machine with a different culture, and in that case it might be different! If you specify explicitly that the string you're processing is culture-invariant, then it will always be the same, regardless of what culture is set on the machine.
You can read more about this topic on MSDN code analysis documentation:
CA1305: Specify IFormatProvider
Trying to generate currency type seed data for an application and iterating through the CultureInfo collection of System.Globalization. I kept getting truncation errors on my insert statements until I discovered that when Caribbean comes up (listed at MSDN as CB) a 3 character value of 029 on the TwoLetterISORegionName. I had a serious WTF moment.
Does anyone know why this happens?
To reproduce:
Instantiate the RegionInfo object for the Caribbean.
RegionInfo region = new RegionInfo(9225);
Access the TwoLetterISORegionName property.
Value should be CB. Actual value is 026.
It looks like there is no ISO-3166 2-letter ISO Country Code based on the ISO Website. The same goes for the Wikipedia ISO 3166-1 page.
It is quite interesting that Carribean is listed by MSDN. Maybe this is because RegionInfo also applies to geographical regions and not only to countries, but that's just a guess.
I'm currently doing an app, that needs to be able to work with the US number layout (123,456.78) as well as with the German layout (123.456,78).
Now my approach is to use NumberFormatInfo.InvariantInfo about like this:
temp = temp.ToString(NumberFormatInfo.InvariantInfo);
this works great when for example reading a number from a textbox. When System is set to English format it will take the . as separator, when it's set to German it will use the ,.
So far so good....but here's the problem: I have a device that returns info in the American format, and that won't change (transmitted via RS232). So I receive something like 10.543355E-00.
Now when on German setting the . will be discarded since it's just the group separator
and the number I will end up with is 10543355....which is a lot more :)
I tried with the same technique thinking this would make the whole thing kind of 'cultureless' to be able to process it independently from the system language but it didn't work :)
I hope you can maybe help me here...I'd love to use a way without having to implement the whole culture stuff etc since all I need here is really numbers that get calculated the right way.
You should use CultureInfo.InvariantCulture when parsing strings from the device. This will cause it to use the invariant culture, which has the US rules for decimal separation.
Edit in response to comments:
The issue is not when you call .ToString(), but rather when you read the string from the device, and convert it to a number:
string inputFromRS232Device = GetDeviceInput();
double value;
// You need this when converting to the double - not when calling ToString()
bool success = double.TryParse(
inputFromRS232Device,
NumberStyles.Float,
CultureInfo.InvariantCulture,
out value);
What is best practice for the scenario listed below?
We have an application which we would like to support multiple currencies. The software will respect the users locale and regional settings to dictate the correct number format, i.e. $10,000.00 or 10.000,00₴ etc.
We would however like to be able to format different numbers based upon a currency ID (perhaps the three letter ISO4217 code). Our idea is to store a table in the database with each currency and then request from the user to select the currency symbol which will be used. The program will then format all numbers based upon the locale/region setting but will use the currency symbol as defined in our program.
So, given the following values and currencies
10000 AUD
5989.34 USD
450 EUR
the program would output
$10,000.00
$5,989.34
€450.00
or with a regional setting that formated numbers as #####,##$ the result would be;
10000,00$
5989,34$
450,00€
Respecting locale/region number formatting but displaying different currencies, what is best practice for this?
I hope this makes sense.
Technology used is c# .NET 2.0.
I think this Q is an excellent and clear answer as to WHAT you should be doing
SO - Proper currency format when not displaying the native currency of a culture
And this Q has a good answer of HOW to do this in C#
SO - Currency formatting
It sounds like you have a handle on the best practices for formatting data. If I were you I would worry less about what is technically the standard but focus more on what it is your users are accustomed to.
If you are operating in markets where users are fairly savvy about different currencies it is a very different thing than having more monocultural users. Depending on the case, you will need to design your interface in a different way.
Moreover, if your requirement is to display any currency to any locale, the ISO4217 standard is your best bet. It is what is shown at currency exchange shops across the world, on currency exchanges, invoices, etc. Otherwise, displaying currency symbols could be rather confusing to some users and the symbol by itself does not indicate what currency the amount actually is.
I would also reference the following SO questions. Not all of them directly relate to your problem, but they have very good meta discussions about the issues involved in supporting multiple currencies.
How do I round up currency values in Java
Representing Monetary Values in Java
What to do with Java BigDecimal performance?
Rather than storing just the currency symbol, you could store the culture string for each currency code, so AUD --> en-AU
CultureInfo ci = new CultureInfo("en-AU");
currencyValue.ToString( "c", ci );
I'm not sure how much flexibility there is in formatting available.
Not sure if this helps:
Formatting Numeric Data for a Specific Culture
You could store the money value as a decimal, then append the currency symbol on the UI.
Fowler discusses the Money pattern in Patterns of Enterprise Application Architecture here: http://www.martinfowler.com/eaaCatalog/money.html
I faced a similar situation. I found a way, dont know how useful it will be for you. But it solved my problems. I set a session string for each place like Session["cur"]="0.000" or "0.00" for each login authentication. And where ever currency comes in the system I used .ToString[Session["cur"].ToString()] to the model variables. It did the trick for me . Hope it helps you too .