Properly formatting currency values based only on ISO4217 currency code? - ios

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.

Related

Different date format and one locale

I use I18n.t('date.formats.default') for date formatting.
The issue is that in different countries there are different date formats, but one english locale.
For example '%m.%d.%Y' fo US and '%d.%m.%Y' for Australia
I need the ideas how to handle with it.
While you might simply use something else for date formats, the easiest drop-in solution would be to store all possible variants in the same string and on retrieval do (assuming the country code is known):
'date.formats.default': 'US[%m.%d.%Y],AU[%d.%m.%Y]'
code = 'AU'
format = I18n.t('date.formats.default')
format[/(?<=#{code}\[).*?(?=\])/] || format
#⇒ "%d.%m.%Y"
The latter || format is needed to support normal format, without brackets.
If you don’t like regular expressions, store the JSON there, containing hash {CODE => FORMAT}, parse it and retrieve the value.
I think it is more convenient way to use different locales.
For example en-AU.yml, en-US.yml, en-CA.yml etc.? Especially i18n supports this.
Australia has different time format too.
Every time you have to take into account all these nuances for each country.
Using different locales simplifies this.

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".

User input money value in grails

I need to get a money value from the user, but the user can type the number in different formats:
1.234.234,78
1234566,26
123,123,132.12
I don't know how to treat the variable.
I have to transform that value in Double type but if the user give me a value with "," the program generate an exception, how can I handle this?
If you want to handle money then I recommend you a great grails plugin with many feature for currencies handling and conversions etc.
Have a look at Currency plugin.
In your domain static constraints use matches with the regular expression that handles currency format.
Then using java.text.NumberFormat allows you to format double regardless of having comma in the input.

How to store user's preferred currency format in Rails application?

I want the users of my international, multilingual Rails app to be able to set their own currency and currency format.
Right now, I am using translation files to achieve this:
de:
currency:
format:
format: "%n %u"
unit: "€"
separator: ","
delimiter: "."
However, this is not flexible enough. What e.g. if a user wants to use the euro but wants the € symbol to appear before the amount?
So what is the best way to store a user's currency formatting preferences and then use it throughout the application?
I figured to store the user's preferences in the database like this:
user.preferences.format = "%n %u"
user.preferences.unit = "€"
user.preferences.separator = ","
user.preferences.delimiter = "."
user.preferences.save
And then, in the view:
number_to_currency(123.50, unit: user.preferences.unit, separator: user.preferences.separator, delimiter: user.preferences.delimiter, format: user.preferences.format)
Is this common Rails practice? What might be a better approach?
Thanks for any help.
I think you've made it better to use individual’s preferences/settings instead of app settings.
But there is still a concern in your new arrangement.
What if an user want to change his currency, say from Euro to Canada Dollar? This will be not so easy in your app because his current amount is in Euro. You need to convert this amount into Can Dollar and take currency rate into consideration which adds complexity. And the amount will not be consistent. For example, if he want to change back to Euro after a few days, it's very likely he will meet a new Euro amount.
My suggestions:
Use a consistent amount and currency for everything. Say the price is always US$ 19. The user can have his own currency, of course, but it will be converted from the base amount which is US Dollar.
Use separate settings for custom currency and save them into db. You've already done that. And there is a very nice gem which could do it efficiently: https://github.com/ledermann/rails-settings

.NET Currency exponent ISO_4217

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...

Resources