Show me the money? How?

by Michael S. Kaplan, published on 2008/07/05 13:29 -04:00, original URI: http://blogs.msdn.com/b/michkap/archive/2008/07/05/8694052.aspx


Regular reader Jan Kučera's latest attempt to thwart my fantasy of emptying the Suggestion Box went something like this:

Hello again,

I'm trying to display prices, in a currency the user chooses. I've abolised keeping my own formatting info for each currency, and decided to assign a culture to the currency, and let the .NET to format the price using its data (I hope this is a good decision :)), so the list of available currencies is like USD -> en-US, GBP -> en-GB etc.

Now the question, what culture should I choose to format the price in euros? Are the formatting rules the same for whole union and I can just randomly choose one country?

Or is there any IFormatProvider for whole EU?

Thanks!
Jan

PS. Well by checking all cultures which has € as a currency symbol I see that every country formats the currency in a different way, so this is not going to work. So is the suggested way to use the USD/GBP strings and format the price as a number using user's culture?

It is funny that the question of formatting is so prominent here -- isn't the big problem the actual conversion? The formatting seems like the easiest issue....

As I mentioned in Show me the [small]money!, I am quite fond of Cloanto Currency Server for the actual conversion.

For the formatting, I would take the NumberFormatInfo of the user's culture, and change the currency symbol, but leave everything else exactly as is. Because a user's expectations of the way numbers work does not change just because they convert, and although some would understand the differences in formatting, others wouldn't. And the ones who wouldn't could easily end up misunderstanding the data they are seeing....

But taking a step back, it is really not such a big deal here.

At least for most situations.

You might be wondering why I say that if I talk about the potential for confusion and such?

Well, it is true that I might misunderstand the currency value if things like decimal places change

But think about the scenario for a moment:

I'm trying to display prices, in a currency the user chooses.

This kind of implies the typical online commerce scenario, when most commonly one is converting from some unknown currency into their own. And in this case, for the desired target of everything being in a user's own currency with their own formatting, they will be getting the same final results either way.

In short, the difference between "formatting with user preferences" and "formatting with the currency's preferences" disappears when dealing with the user's own currency.

And of course as I mentioned in Show me the [small]money!, a method to get the culture from the currency doesn't exist in the .NET Framework or Windows, so unless you deal with a third party product or do all of the work yourself, the whole "formatting with the currency's preferences" is hardly going to be all that easy to do anyway.

This may be why Jan was doing it himself, in fact!

So now I will take yet another step back and suggest finding some component that does all of the work here. Money gets one into all kinds of regulatory rules and laws about commerce. It is much better to find a component that can help indemnify you if they screw it up, rather than being ruined if one makes a mistake.

Strange advice coming from someone who works for a company with such a huge "let's develop everything here" bent on things, but (a) Microsoft has the time and money to spend on such a plan as wellas money to pay for their mistakes when they make them, and (b) there are several cases where I think they are wrong in this approach....

 

This blog brought to you by $ (U+0024, aka DOLLAR SIGN)


# Mihai on 8 Jul 2008 11:59 AM:

<<In short, the difference between "formatting with user preferences" and "formatting with the currency's preferences" disappears when dealing with the user's own currency.>>>

I don't get this.

The fact that I prefer to use Euro tells nothing if I want the currency formatted by French, Swiss, or German rules.

# Michael S. Kaplan on 8 Jul 2008 12:53 PM:

If you select French then you probably won't customize the formatting (since most people don't!) -- and then what we have as your currency preference and what we have as the currency's preference match.

Voilà, for the common cases. :-)

# Jan Kučera on 8 Jul 2008 1:11 PM:

Hi Michael, I'm very sorry I've vitiated your efforts! (yet still I can't figure out how the 'thwart' thing is pronounced.. :-))

As you have guessed, this is an online commerce scenario. Well I understand that user is more friendly with numbers formatted using their own culture, but in the web world, this is a bit problem, as -being on the server- you have no idea what his culture is. Yes I know there is accept-language hint, but I am not very comfortable relying on it. Then, the user is free to change the language on the web site besides this.

What I've tried is to check the language the user was fallbacked to (or has chosen). If the corresponding culture has euro as a currency, I go ahead and use it for formatting. If not, I search through the accept-languages and if any of them represents culture with euro, I use it to format the value. If not, I use the InvariantCulture and replace the ¤ with EUR . It works well, but as I'm reading your post, maybe I'm making the problem more complicated than it is. I just wanted to use national currency symbols and make it right, but this probably just isn't possible on the web.

The user has chosen the language, why not just use its formatting if it is a user's choice? Well, if the web is say in Czech and English US, and you come from England, than you would probably be happy enough with the English US version. But I do sell in british pounds, so you choose to display pounds. Now, should I format the number as a currency in en-US and replace the CurrencySymbol with £?

After reading your reply, I think I should.

I did not wanted to touch the exchange rates, this is why I have not mentioned anything about commerce or web. Actually our banks provide current and past rates online in XML, so this is not that big issue for me.

And last thing you have mentioned as well, I prefer to make things on my own, if not available from Microsoft, and really don't go into "3rd party" labeled things until you know.... I don't know, I have never had to yet. I just don't trust 3rd parties. As extra points I can learn lot of things by trying it myself (and vaste lot of time, I know).

Maybe I'm just too young.

Thank you for your time and reply, and good luck with the suggestion box fantasies!


Please consider a donation to keep this archive running, maintained and free of advertising.
Donate €20 or more to receive an offline copy of the whole archive including all images.

go to newer or older post, or back to index or month or day