.NET Currency exponent ISO_4217 - currency

I'm developing something for international use. Wondering if anyone can shed any light on whether the CultureInfo class has support for finding currency exponents for particular countries, or whether I need to feed this data in at the database level.
I can't see any property that represents this at the minute, so if anyone knows definitively if it exists, before I look for it / buy it from ISO.
Currency Exponent is the minor units of the currency.
http://en.wikipedia.org/wiki/ISO_4217 - e.g. UK is "2"

Take a look at this blog post on getting CultureInfo for a region. Basically, Window and .NET know about the user's region but not their currency. A region implies a currency, but a country can have more than currency. For example, a person in Cambodia would more than likely want to enter and use USD than Riel. If possible, when capturing any currency amount in a multi-currency system you should capture the currency ISO code.
If you just want to make a quick guess, you can create a CultureInfo object and use it's NumberDecimalDigits property. The also creates a problem when countries switch currencies. For example, if Belarus joins the EU, then it's currency would change from BYR to EUR. It's currency symbol and exponent will be out of date.

I looked at this question and provided a solution which may or may not meet your needs here: http://www.codeproject.com/KB/recipes/MoneyTypeForCLR.aspx#CurrencyType
The short of it: I implemented the ISO spec as a custom type using the spec itself to generate the values. Obviously this would need to be regularly updated in production...

Related

Google AdWords conversion value format

I'm using google tag assistant in my browser, but google tag assistant always shows
Conversion Value should be prefixed with standard currency.
i think the reason is the decimal places value at image tag is not picked up:
<img height="1" width="1" style="border-style:none;" alt="" src=""https://www.googleadservices.com/pagead/conversion/xxx/?label=xxx&value=267500·00&currency_code=IDR&guid=ON&script=0""/>
first i used plain . as decimal places seperator, and then i used · and ¸, but none of this work
Google Tag Manager is a great tool, but I wouldn't take all of its recommendations at face value.
Adwords itself does have a bit of a storied past when it comes to the meaning of conversion values. For a long time, the value of conversion was just a number with no explicit monetary semantic.
As this is not an optimal setup if you are a multinational advertiser that has to deal with problems such as marketing budgets being denominated in local currencies, the option to define a currency for conversions was added.
Note, however, that the conversion value columns in the Adwords interface still show no currency and that the ConversionValue field in the API still has type Double – as opposed to the Cost field, which is of type Money.
Thus, as far as I can tell, the only point where conversion value currencies are important is for situations where a conversion occurs that has a different currency than the billing currency of the Adwords account to which the conversion belongs. In that case, the amount will be converted between the currencies automatically. In all other cases, the conversion value is a number that doesn't care about its currency.
So, to summarize: if you use the same currency for both billing and reporting currencies (and don't use things like MCC-level conversion tracking for accounts with different currencies), you'll be fine.
To fix this warning google_conversion_value on script tag should be change or add.
var google_conversion_value = 'currencyhere' + valuehere;
eg
var google_conversion_value = 'IDR' + 267500.00;
This warning is very misleading as there's a seperate variable that they can use
google_conversion_currency but they require as to append a "currency" prefix on the "value".

Information for localization

Is there a list with complete information about caracteristics like:
currency
date and time (including if it is 12 or 24 hours) format
measurement units (distance, speed, temperature...)
preferred language
masks for phone and local documents
timezones (at least the main ones / variations if daylight saving time is applicable)
decimal and thousand separators
for countries around the world?
I am doing it myself, however, as it takes too long to gather the data, I tought maybe someone have already have it done.
Don't reinvent the wheel.
Start with CLDR, the Common Locale Data Repository (http://cldr.unicode.org/)
Or if you want to honor the locale preferences in your application, use standard I18N APIs (from you platform, whatever that is, or a popular library, like ICU, http://site.icu-project.org/)
For currencies you can rely on international standard ISO 4217. It also refers to the country code of each currency code. This website provides this dataset for download.
For date formats, the best reference seems to be wikipedia.
The measurement units is a very complex domain, because you need to know which dimension you measure (speed, distance, volume, ...) and the units (paper size in cm is not the same as road distance in km). Here you have some lists per type of units, but not per country. This website shows a list of system of measurements in use per country. You'll see that fortunately ùany of them share the metric system, so taht you could use an approach "by exception" documenting yourself only on the remaining ones".
For languages, you have international standard ISO 639 or IANA , but it's country independent. You can look at reference lists for locale such as here: it associates a language code to a country code, so that you could complete the standard information. Note that some countries have several language, and you cannot and should not decide which one is preferred.
For telephone masks, there is only an international list of prefix. The usage vary greately accross countries. Some have fixed format, some use variable formats, some have zone prefixes and some not. Sometimes there is even no clear standard in the country and there are several coexisting usages. I'm not aware of any global list of these.
For timezones around the world, you could have a look at IANA which is extremely comprehensive.
For decimal and thousand separators, it's not an international standard. Again I'd suggest to refer to Wikipedia

Properly formatting currency values based only on ISO4217 currency code?

I am using a web API that returns values like this:
{
"CNY": 42.0,
"DKK": 31337.00,
"EUR": 6789.01,
"GBP": 6502.00,
"USD": 12345.67,
...
}
I would like to format these values as proper currency values (e.g. for USD, "$12,345.67," for EUR, "6789.01€", for GBP, "£6502.00", for DKK, "31337.00 DKK", etc.) In other words, using the proper symbol ($, £, etc.), but also for some currencies, the symbol comes before the amount whereas for others, it comes after the amount, etc.
The catch is the API I am using only returns ISO4217 country codes ("USD," "EUR," "CNY," etc.) instead of locales. I know NSNumberFormatter has an NSNumberFormatterCurrencyStyle which looks like it does the proper formatting, but apparently the only way you can tell it what type of currency you're dealing with is by handing an NSLocale to its setLocale.
Is there any way to convert from ISO4217 currency codes to locales, other than hardcoding a ISO4217->locale conversion table in my code (which, granted, might work ok since ISO4217->locale mappings probably don't change very often??) Still, I would prefer a way of doing this programmatically if possible.
Of course you cannot map currency codes to locales - with the Euro this just must fail because there are probably 50 locales using Euro.
What you need to do is display the currency according to the locale of the user. Users in Germany, Britain, USA, can all display Euros, for example, and do it in different ways. Each locale can display all currencies.

Common sense when storing currencies?

After reading up on how to best handle users in multiple timezones properly, I've learned that the way to go is to store all dates in an normalized, application-wide timezone - UTC and then apply the diff between the normalized timezone and the individual users timezone when outputting. Today I came to think if this would be appropriate to apply this approach to handling currency in software:
All stored currency are converted to a application-wide currency, lets say EUR (€), and when outputting, the currency is converted back into the users own currency, with an updated exchange rate of the day?
What's common sense here? How is this generally solved and what should I be aware of before choosing a way to handle this?
One standard approach is to store both an amount and a currency whenever monetary values are held and manipulated.
See the Money Pattern in Martin Fowler's Patterns of Enterprise Application Architecture.
Fowler describes defining a simple datatype to hold the two primitive components, with overloaded arithmetical operators for performing monetary operations:
"The basic idea is to have a Money class with fields for the numeric
amount and the currency. You can store the amount as either an
integral type or a fixed decimal type. The decimal type is easier for
some manipulations, the integral for others. You should absolutely
avoid any kind of floating point type, as that will introduce the kind
of rounding problems that Money is intended to avoid. Most of the time
people want monetary values rounded to the smallest complete unit,
such as cents in the dollar. However, there are times when fractional
units are needed. It’s important to make it clear what kind of money
you’re working with, especially in an application that uses both
kinds. It makes sense to have different types for the two cases as
they behave quite differently under arithmetic.
Money needs arithmetic operations so that you can use money objects as
easily as you use numbers. But arithmetic operations for money have
some important differences to money operations in numbers. Most
obviously, any addition or subtraction needs to be currency aware so
you can react if you try to add together monies of different
currencies. The simplest, and most common, response is to treat the
adding together of disparate currencies as an error. In some more
sophisticated situations you can use Ward Cunningham’s idea of a money
bag. This is an object that contains monies of multiple currencies
together in one object. This object can then participate in
calculations just like any money object. It can also be valued into a
currency."
The difference between handling time and currency, is that time zones doesn't shift in value.
When handling monetary values you have to consider what the currency of the actual money is. If the actual money is in USD and you store it as EUR, you will get a discrepancy between the actual value and the stored value when their values shift.
Alternatively you would have to recalculate all values when the exchange rate is updated, but that would take away the purpose of storing the values in a single currency.

F#: Can units of measure be bound dynamically at runtime?

I'm very new to F# and am intrigued by the Units of Measure functionality and have a rough idea of how it works normally, but would like to know if it's possible to bind measures to values where we don't know what the measure will be until the code is executing?
The practical example I'm looking at is binding floats as currency values where the unit of measure is inferred from a database lookup.
Let's assume that the measures for each currency (USD, EUR, AUD, etc) are declared normally:
[<Measure>] USD
[<Measure>] EUR
[<Measure>] AUD
...
First you would need a way to obtain a measure's type from an identifier, ideally the measure name itself as the currency code is most likely stored and retrieved as a 3-character string (similar to Enum.Parse()).
Then you would need a way of binding a float value to the type created in the previous step.
Is this possible, or is there another way to achieve the same outcome?
This isn't possible, since F# units-of-measure are erased (they only exist at compile-time).
You could author a library with a runtime implementation (I haven't thought about what a design would look like). But you probably lose the static checking.
I think possibly a better strategy may be to isolate the boundary, and at the boundary point (where you read from the database and infer the unit types) somehow get the right types into the type system, but depending on how the code is structured and what exactly you're doing, that may or may not be possible/easy...
Unless you are writing code that is actually specific to one particular currency, you shouldn't explicitly mention USD, EUR, AUD etc in your code. Instead, make your code polymorphic over the currency/currencies involved.
What you have to think about is what kinds of safety you are expecting to get from units of measure. If for example (in a very simplistic scenario) you would be reading from a database field, doing some processing and writing back to that same field, then having a function of type float<'a> -> float<'a> is exactly what you want: you don't care what the currency is, so long as you get back the same one you put in.

Resources