Currency format and localization

Gloops

Well-known member
Joined
Jun 30, 2022
Messages
137
Programming Experience
10+
Hello everybody,

I have a problem that appears in WinForms, but as the currency format is involved I think the context is larger.

I have a unit price that is displayed with format C2, i.e. currency with two decimal digits.

OK. At execution I realize that this format ends with System.Globalization.CultureInfo.CurrentUICulture.NumberFormat.CurrencySymbol.
Here in Europe this is set to "€".

But I use Northwind database, that shows the products of an American seller. I presume his prices are labelled in dollars, and that a later conversion is required —even if nowadays the difference will not be enormous.

What is the best way to express that this field shows prices in dollars, and that a calculated column further shows euros ?

I am tempted by a personalized format, and for the field I have set it to "#,##0.00 $"

At execution it shows what I planed, nicely with the comma replaced by a space and the dot by a comma, even if the graphical programming interface warns that the exchange with the database has to be double sided.
Anyway, the format does not affect the value, does it?
I mean it only affects how humans will understand it.

In a DataGridView there is no splitting problem. Here in the forum I am supposed to put an unbreakable space, to avoid the $ sign to be displayed on the following line when you display with a narrower window, am I not? How do I do that?
 
Last edited:
Proper formatting would require the $ symbol to be displayed before the value rather than after. When you use a format specifier like "C2", the specific format will be determined from an IFormatProvider object. If you don't specify one, the current culture will be used. If you don't want to use the current culture then you should specify the culture you do want. If you're using a DataGridView then you are presumably setting the DefaultCellStyle.Format property of the column to "C2". You can assign an appropriate IFormatProvider to the DefaultCellStyle.FormatProvider property. That might be done like so:
C#:
var cellStyle = myColumn.DefaultCellStyle;

cellStyle.FormatProvider = CultureInfo.GetCultureInfo("en-US");
cellStyle.Format = "C2";
 
Back
Top Bottom